【问题标题】:Run NSBundle from the documents folder从文档文件夹运行 NSBundle
【发布时间】:2011-07-03 13:53:03
【问题描述】:

有什么方法可以在 iOS 上的文档文件夹中使用NSBundle

【问题讨论】:

  • 我不明白这个问题。你到底想做什么?

标签: ios


【解决方案1】:

也不确定确切的问题是什么,但这是我访问我的应用程序的本地文档文件夹的方式(这不是您存储应用程序使用的资源的文档文件夹,而是您的应用程序存储本地资源的文件夹) 例如,在我的应用程序中,我使用相机拍摄照片并将它们存储到应用程序的本地文件夹,而不是设备相机胶卷,因此要获取我这样做的图像数量,请在 viewWillAppear 方法中使用:

// 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]);
// ---------------

如果您想知道文档文件夹中的内容(您可以在 iOS 模拟器中实际浏览的文件夹

通过 ~/YourUserName/Library/Application Support/iPhone Simulator/versionOfSimulator/Applications/appFolder/Documents)

你可以改用NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];

希望对你有帮助,伙计!

【讨论】:

    【解决方案2】:

    我不完全确定你在做什么,但就使用应用程序包中的文件而言,一般方法是将其复制到文档目录中,如下所示:

    1. 检查(在首次启动、启动或根据需要)您的文档目录中是否存在该文件。

    2. 如果不存在,请将文件的“安装”版本从您的包中复制到文档目录中。

    就一些示例代码而言,我有一个用于以下目的的方法:

    - (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;
    }
    

    这将检查您的文档目录中是否存在指定文件,如果该文件不存在,则从您的包中复制该文件。

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2011-12-24
      • 2016-05-12
      • 2013-05-17
      相关资源
      最近更新 更多