Metareven
Metareven

Reputation: 842

Using nibs from cocoapods in swift 4

I have made a framework that contains a function that displays a login view according to a xib that is contained in that framework. Both the xib file and the swift file for the view are called AuthenticationViewController

However when I try to use this function in another project that uses this as a pod, it fails with "Could not load NIB in bundle...(not yet loaded)' with name 'AuthenticationViewController'"

The view is being shown by the following code that is located in my pod/framework:

func authenticate(viewController: UIViewController){
    let bundle = Bundle(for:AuthenticationViewController.self)
    let newViewController = AuthenticationViewController(nibName:"AuthenticationViewController" , bundle: bundle)

    viewController.present(newViewController, animated: true, completion: nil)
}

What is the issue here? Is there supposed to be a separate bundle for my pod, because I only get one bundle when calling:

Bundle.allBundles

My .podspec file contains the following section:

s.resource_bundles = {
"MyPodName" => ["MyPodName/*.xib"]

}

but I have tried to load the bundle using:

Bundle(identifier:"MyPodName")

and that does not work either.

How are you supposed to use nibs from pods?

Upvotes: 1

Views: 657

Answers (2)

Metareven
Metareven

Reputation: 842

The problem was that I was using:

s.resource_bundles = {
"MyPodName" => ["MyPodName/*.xib"]
}

This does not seem to work with the approach i had done. So I had to change podspec file to:

s.resources = ["MyPodName/*.xib"]

That made everything work

Upvotes: 1

FedeH
FedeH

Reputation: 1373

I seem that the bundle is not the correct one. I use this function in my pod library in order to get the bundle

- (NSBundle *)getBundle {
    NSBundle *podBundle = [NSBundle bundleForClass:self.classForCoder];
    NSURL *podBundleURL = [podBundle URLForResource:@"MyPodName" withExtension:@"bundle"];
    NSBundle *bundle = [[NSBundle alloc] initWithURL:podBundleURL];
    return bundle;
}

Once you have the bundle you can load the view. Also, run a pod install after you edit the .podspec and add a new file to reorganize the files.

Upvotes: 0

Related Questions