Skip to content

Instantly share code, notes, and snippets.

@laullon
Created June 19, 2013 15:58
Show Gist options
  • Save laullon/5815481 to your computer and use it in GitHub Desktop.
Save laullon/5815481 to your computer and use it in GitHub Desktop.
Embedding a Core Data model from a static library project http://spinside.blogspot.com.es/2012/12/embedding-core-data-model-from-static.html
Embedding a Core Data model from a static library project
In one of my project, I want to share a Core Data model between 2 projects. I have a common static library project where the model and classes are defined.
SITUATION
a common project that builds a static library which contains my Core Data model (to be shared with an iOS and an OSX project)
my main iOS project
PROBLEM
static library cannot include resources
how can I give make my main project use the Core Data model ?
SOLUTION
Create a new target of type Bundle (in OSX/Frameworks and Library)
Change the build settings of this target:
Base SDK: Latest iOS
Supported platform: iOS
Valid architecture: arm architecture
Add your model file (.xcdatamodeld) to the Build phases / Compile Sources section of the bundle target
Link your bundle with Core Data (add it to Build phase / Link Binary section)
Build the bundle target
Now in your main project:
Select the project to show Build Phases / Copy Bundle Resources
Drag the Bundle target product (from Products group in the project navigator) to the Copy Bundle Resources section
If it s not already there, add your static library: in Build Phases / Link binary section, use the "+" button to add your static library file
Load the model from the bundle:
NSString *staticLibraryBundlePath = [[NSBundle mainBundle] pathForResource:@"ModelBundle" ofType:@"bundle"];
NSURL *staticLibraryMOMURL = [[NSBundle bundleWithPath:staticLibraryBundlePath] URLForResource:@"MyDataModel" withExtension:@"momd"];
model = [[NSManagedObjectModel alloc] initWithContentsOfURL:staticLibraryMOMURL];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment