When you develop for iOS, you can quickly add third-party libraries to your NativeScript projects via the CocoaPods dependency manager.
To work with such libraries, you need to wrap them as a custom NativeScript plugin and add them to your project.
You need to install CocoaPods. If you haven't yet, you can do so by running:
$ sudo gem install cocoapodsNOTE: All operations and code in this article are verified against CocoaPods 0.38.2.
To check your current version, run the following command.
$ pod --versionTo update CocoaPods, just run the installation command again.
sudo gem install cocoapods
To start, create a project and add the iOS platform.
$ tns create MYCocoaPodsApp
$ cd MYCocoaPodsApp
$ tns platform add iosFor more information about working with NativeScript plugins, click here.
cd ..
mkdir my-plugin
cd my-pluginCreate a package.json file with the following content:
{
"name": "my-plugin",
"version": "0.0.1",
"nativescript": {
"platforms": {
"ios": "1.3.0"
}
}
}Create a Podfile which describes the dependency to the library that you want to use. Move it to the platforms/ios folder.
my-plugin/
├── package.json
└── platforms/
└── ios/
└── Podfile
Podfile:
pod 'GoogleMaps'
Next, install the plugin:
tns plugin add ../my-pluginNOTE: Installing CocoaPods sets the deployment target of your app to iOS 8, if not already set to iOS 8 or later. This change is required because CocoaPods are installed as shared frameworks to ensure that all symbols are available at runtime.
tns build iosThis modifies the MYCocoaPodsApp.xcodeproj and creates a workspace with the same name.
IMPORTANT: You will no longer be able to run the
xcodeprojalone. NativeScript CLI will build the newly created workspace and produce the correct package.
In case of post-build linker errors, you might need to resolve missing dependencies to native frameworks required by the installed CocoaPod. For more information about how to create the required links, see the build.xcconfig specification.