Use an Objective C library in NativeScript for Angular iOS app

InstructorNathan Walker

Share this video with your friends

Send Tweet

We will learn how to install an Objective C library distributed via a CocoaPod into our project and expose its native api to our app. Specifically, we will cover how to immediately start working with the library even when a NativeScript plugin does not exist for it. Take for example Filestack, a really powerful file uploading api that provides an SDK written in JavaScript for the web, but also offers native SDK's for iOS and Android. Let's take a look at how to code right against this native iOS SDK.

Vance
~ 4 years ago

You have to specify a target when installing cocoapods now. This requires an XCode project....This is the new syntax

project "../../../platforms/ios/ngnative.xcodeproj" target "ngnative" do pod "FSPicker"

end

http://stackoverflow.com/questions/34556991/pod-install-displaying-error-in-cocoapods-version-1-0-0-beta-1

Nathan Walkerinstructor
~ 4 years ago

You actually don't need to specify the target, it works as shown in the lesson with no target. Just make sure you have at least 1.2.0 of Cocoapods installed, find out with:

pod --version

Vance
~ 4 years ago

Hm, I'm on 1.2 and still says target required. doing it this way fixed it. I also had to add path to project.

project "../../../platforms/ios/ngnative.xcodeproj" target "ngnative" do pod "FSPicker" end

Nathan Walkerinstructor
~ 4 years ago

It might just be because you need to specify the version number and the target platform. Try clearing your platform with rm -rf platforms, then replace your Podfile with this:

platform :ios, '8.4'
pod "FSPicker", "~> 1.1.3"

And try that.

Vance
~ 4 years ago

P.S. love that cursive font. Wish IntelliJ had that-- maybe it does, so many plugins!

Nathan Walkerinstructor
~ 4 years ago

Oh yes thanks loooove it!! It's just the Operator Mono font, purchase only - but worth it!

Vance
~ 4 years ago

thanks. Bear with me. So I was able to add the pod fine, but when I compile my main project via tns I get this error: [!] The target ngnative is declared twice for the project ../../../platforms/ios/ngnative.xcodeproj.

The project was made with the CLI tool from the intro to NS tutorial on egghead. When I open it in XCode, I see that all targets match, and there's only one item in the dropdown under "targets" called ngnative (matching the name of the project). My target in XCode is also 10.2, matching my podfile. I was not able to install the pod without specifying project path and name...

project "../../../platforms/ios/ngnative.xcodeproj" target "ngnative" do

platform :ios, '10.2' pod "FSPicker", "~> 1.1.3"

end

Nathan Walkerinstructor
~ 4 years ago

Vance, your Podfile should look like this exactly:

platform :ios, '8.4'
pod "FSPicker", "~> 1.1.3"

No more, you don't need target or project line. The error you are receiving sounds directly related to your incorrect Podfile. Clear everything:

rm -rf hooks node_modules platforms

Then after you have corrected your Podfile as seen above, then:

tns run ios --emulator
Vance
~ 4 years ago

Don't mean to be a broken record here... but I still get "[!] The dependency FSPicker (~> 1.1.3) is not used in any concrete target." if I leave out the target. Here, I created a brand new project with tns create. I put in a brand new platforms/ios/ folder with a new podfile.

platform :ios, '8.4'
pod "FSPicker", "~> 1.1.3"
$ pod install
Analyzing dependencies
[!] The dependency `FSPicker (~> 1.1.3)` is not used in any concrete target.
$ pod --version
1.2.0
Nathan Walkerinstructor
~ 4 years ago

Are you manually running 'pod install'?

Vance
~ 4 years ago

DUUUUUHHHHHHHHH. OMG I must have conflated that step with some other tutorial project. just doing a tns add from the root solves it. facepalm thanks for calling out the obvious!

Vance
~ 4 years ago

woohoo! success. thanks! Now, I have a question that might be more interesting to you than my thrashing... http://stackoverflow.com/questions/43057786/how-to-convert-a-cocoapod-to-typescript-when-theres-no-viewcontroller-exposed

Nathan Walkerinstructor
~ 4 years ago

Hooray! Great to hear you got it working now :) Also sounds like you got your SO question answered yourself as well, that correct?

i was passing the viewController instead of the view. so pass rootVC().view

:)

Sandesh
~ 3 years ago

Hi Nathan, Do you have an example video where in you create a Nativescript app using Native iOS library without using Cocoapods? Say, I have to include 'myTest.framework' library into a Nativescript app without using Cocoapods. Any help is greatly appreciated

Ricky Brown
~ 2 years ago
Marian
~ 2 years ago

Hi Nathan, When only watching this, I thought it's so clear... but then, when trying to actually do it... Trying to create a TwilioChat plugin, and looking in their docs to get the Objective C code... it looks completely different and seems like nothing from this apply ( https://www.twilio.com/docs/chat/tutorials/chat-application-ios-obj-c ) Looks to be a case-by-case thing, and you can't possibly do a tutorial for all pods out there :( Watched the course 2 times and I still have no idea how to write this (plus "requestTokenWithCompletion" and "initializeClientWithToken"):

(void)connectClientWithCompletion:(StatusWithErrorHandler)completion {
  if (self.client) {
    [self logout];
  }

  [self requestTokenWithCompletion:^(BOOL succeeded, NSString *token) {
    if (succeeded) {
      [self initializeClientWithToken:token];
      if (completion) completion(succeeded, nil);
    }
    else {
      NSError *error = [self errorWithDescription:@"Could not get access token" code:301];
      if (completion) completion(succeeded, error);
    }
  }];
}

P.S. Also, not sure what's with this "self", but it doesn't look like it's the root view controller, like in the video, because rootVC doesn't have the "client" property. I guess that writing plugins is only for those that also know Objective C or Swift. Although, one of the main advantages of NativeScript should be to not have to learn that, to simply use TypeScript/JavaScript and even a framework of your choice (Angular/Vue)