Run NSBundle from the documents folder

前端 未结 2 1651
悲&欢浪女
悲&欢浪女 2020-12-22 11:33

Is there any way to use a NSBundle from the documents folder on iOS?

2条回答
  •  伪装坚强ぢ
    2020-12-22 12:08

    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!

提交回复
热议问题