Reputation: 12036
I want to punt my sqlite db in to a custom bundle and use that bundle in more than one project.
I have created a new target from Mac OS X FrameWork&Library -> Bundle, I have build the target and the I have put my sqlite db in the bundle. I have added the bundle to my iOS project and try to read the db from it.
Now comes the problem: I can get the bundle and read its identifier but when I try to get the path for db I get null return string.
The code to read db path:
NSBundle *myDbBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:@"myDbBundle" ofType:@"bundle"]];
NSLog(@"BundleIdentifier:%@",[myDbBundle bundleIdentifier]);
NSString *databasePath = [myDbBundle pathForResource:@"myDbName" ofType:@"sqlite"];
NSLog(@"DataBasePath:%@",databasePath);
Any sugestion, docs or tutorial about this issue are wellcome.
Update: It seems that I can not load the bundle that is why when I try to read from it I fail.
Code:
NSError *error;
NSLog(@"LoadBundle:%@",[myDbBundle loadAndReturnError:&error]?@"yes":@"no");
NSLog(@"Error:%@",[error localizedDescription]);
So the real problme is how do I create a bundle in witch I place a sqlite db and witch can be read from ios app? To create the bundle I used the xcode template under Mac OS X so I think that I have to change something in the build settings, but I don't know what.
Upvotes: 1
Views: 4178
Reputation: 27211
Swift 3
let dbname = Bundle.main.path(forResource: "horoscope-release", ofType: "sqlite")!
Upvotes: 0
Reputation: 4131
NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"myDbName" ofType:@"sqlite"];
should be good enough to get the path to your myDbName.sqlite inside your main bundle
EDIT: Okay, here is what I did, first, New File -> Resources -> Settings Bundle, create a new one, name it myDbBundle, then right click on that newly created bundle, add myDbName.sqlite in it, make sure that it's not inside en.lproj. Also, make sure when you create a new bundle, you tick the targets to include it into your current target, since this is not Settings.bundle, you want to include it. Then run you code, it works fine.
and here is the code I use
NSBundle* bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"myDbBundle" ofType:@"bundle"]];
NSLog(@"%@", bundle);
NSString* test = [bundle pathForResource:@"myDbName" ofType:@"sqlite"];
NSLog(@"%@", test);
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:[bundle pathForResource:@"Root" ofType:@"plist"]];
NSLog(@"%@", dict);
the NSLog(@"%@", dict);
is printing out correct value, which means it's successfully loaded
Upvotes: 4
Reputation: 7493
I haven't done this myself, but this blog seems to be what you are trying to do Universal framework iPhone iOS 2.0
It describes creating a framework that can be used to package up any files and then simply imported into other projects as needed.
Upvotes: 1