MajorShepard
MajorShepard

Reputation: 433

How to access Bundle from iOS static library

I'm working on a static library using Core Data framework. Sadly, I'm unable to access my Bundle / .momd file generated.

What I've done :

My not working code :

NSString *staticLibraryBundlePath = [[NSBundle mainBundle] pathForResource:@"sdkResources" ofType:@"bundle"];
NSURL *staticLibraryMOMURL = [[NSBundle bundleWithPath:staticLibraryBundlePath] URLForResource:@"Model" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:staticLibraryMOMURL];

Returns :

[FAILED], NSInvalidArgumentException "Cannot create an NSPersistentStoreCoordinator with a nil model" raised

The problem seems to be the "mainBundle" access because the code below is working when I run my sdk tests (at least no error)

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *staticLibraryMOMURL = [bundle URLForResource:@"Model" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:staticLibraryMOMURL];

But when I want to use my sdk inside an other App (using a podFile file) I can't access my bundle and get the same error :/

I've read and try a lot but nothing seems to work. I don't have a lot of experience in iOS dev and I'm trying my best to understand what's going on but it's a dead end to me now :(

Regards

Bundle build phase

sdk scheme configuration

Sdk build phase

I don't know why sdkResources.bundle is red.. Hope this is what you're asking for, thanks.

Edit : After fixing path to sdkResources.bundle enter image description here

Edit :

Ok, I'm really close to the solution, my bundle is successfully copied inside my final App but I can't access my Model and it's only working inside my sdk tests when I'm using [NSBundle bundleForClass:[self class]] instead of [NSBundle mainBundle]. It's probably just a stupid configuration but.. yeah.. I'll keep digging !

Upvotes: 2

Views: 3775

Answers (4)

Mohamed Saleh
Mohamed Saleh

Reputation: 3317

I think the solution is may be much simpler if you are not using Pods and using the framework only. You will have to change the Bundle you are looking for your momd in.

Please check this article here

The solution proposed is

    lazy var managedObjectModel: NSManagedObjectModel = {
    // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
    let carKitBundle = NSBundle(identifier: "com.andrewcbancroft.CarKit")

    let modelURL = carKitBundle!.URLForResource("CarModel", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

Here com.andrewcbancroft.CarKit the bundle identifier of your framework and that is the bundle that contains the momd file that you want to access from your code

Upvotes: 0

MajorShepard
MajorShepard

Reputation: 433

Ok, I got it !

After (too) many hours searching to put my Model inside a Bundle I decided to just keep it inside my sdk and check my podspec configuration (since it was working in my sdk tests but not in my final project)

This is my solution, inside my podspec file I just added :

s.resources      = 'sdk/**/*.{xcdatamodeld,xcdatamodel}'
s.preserve_paths = 'sdk/**/*'
s.framework = 'CoreData'

Basically, I'm just adding my model as a resources and CoreData framework. At least I learned a lot about Bundle and podspec configuration...

Upvotes: 3

Daij-Djan
Daij-Djan

Reputation: 50129

From the screenshot we see that while the bundle is meant to be copied, it doesn't exist.
=> THAT is fine it will be built later maybe

BUT

as we can also see the sdk target has no dependency to sdkResoures.
the bundle is NOT built in time

setting it to be build in the scheme doesn't do it (the scheme would be too late!)
target needs to depend on the bundle target

Upvotes: 0

davbryn
davbryn

Reputation: 7176

If it is red, the file doesn't exist. If you are sure it exists, delete the red file and add it again: This is likely a misconfigured file link

Edit to add: often, you have to build the library first in order to generate the file. Is there a target for this library? If so, select it, build and then switch back to your project

Upvotes: 0

Related Questions