Run NSBundle from the documents folder
Is there any way开发者_开发问答 to use a NSBundle
from the documents folder on iOS?
Not sure what the exact question is, either, but here is how I access the local document folder of my app (this is not the documents folder where you store sources your app uses, but the one your app stores local resources)
for example, in my app I take pics with the camera and store them to the app's local folder, not the device camera roll, so to get the number of images I do this, in the viewWillAppear
method use:
// create the route of localDocumentsFolder
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//first use the local documents folder
NSString *docsPath = [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()];
//then use its bundle, indicating its path
NSString *bundleRoot = [[NSBundle bundleWithPath:docsPath] bundlePath];
//then get its content
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
// this counts the total of jpg images contained in the local document folder of the app
NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.JPG'"]];
// in console tell me how many jpg do I have
NSLog(@"numero de fotos en total: %i", [onlyJPGs count]);
// ---------------
if you want to know what is in the documents folder (the one you can actually browse in the iOS Simulator
via ~/YourUserName/Library/Application Support/iPhone Simulator/versioOfSimulator/Applications/appFolder/Documents)
you would use NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
instead.
Hope it helps you, mate!
I'm not entirely sure what you're getting at, but the general approach as far as using a file from your application's bundle is to copy it into the document directory as follows:
Check (on first launch, startup or as required) for the presence of the file in your document directory.
If it's not present, copy the "install" version of the file from your bundle into the document directory.
In terms of some sample code, I've a method I use for such purposes as follows:
- (BOOL)copyFromBundle:(NSString *)fileName {
BOOL copySucceeded = NO;
// Get our document path.
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];
// Get the full path to our file.
NSString *filePath = [documentPath stringByAppendingPathComponent:fileName];
NSLog(@"copyFromBundle - checking for presence of \"%@\"...", fileName);
// Get a file manager
NSFileManager *fileManager = [NSFileManager defaultManager];
// Does the database already exist? (If not, copy it from our bundle)
if(![fileManager fileExistsAtPath:filePath]) {
// Get the bundle location
NSString *bundleDBPath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
// Copy the DB to our document directory.
copySucceeded = [fileManager copyItemAtPath:bundleDBPath
toPath:filePath
error:nil];
if(!copySucceeded) {
NSLog(@"copyFromBundle - Unable to copy \"%@\" to document directory.", fileName);
}
else {
NSLog(@"copyFromBundle - Succesfully copied \"%@\" to document directory.", fileName);
}
}
else {
NSLog(@"copyFromBundle - \"%@\" already exists in document directory - ignoring.", fileName);
}
return copySucceeded;
}
This will check for the presence of the named file in your document directory and copy the file from your bundle if it doesn't already exist.
精彩评论