【问题标题】:How to access files in interior of an iPhone App?如何访问 iPhone 应用程序内部的文件?
【发布时间】:2010-04-30 08:20:35
【问题描述】:

我需要在 iPhone 应用程序中访问应用程序构建时使用的文件(.plist 等)。有一种硬编码的方法可以做到这一点:

NSString *appDir = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
                      objectAtIndex:0] 
                     stringByDeletingLastPathComponent]
                    stringByAppendingPathComponent:appFolder];

其中appFolder 是文件夹应用程序的名称,例如“test.app”。知道appDir后,访问文件就简单了。

是否有任何其他非硬编码的方式来访问应用程序中的文件?

提前致谢!

【问题讨论】:

    标签: iphone objective-c file-access


    【解决方案1】:
     NSString* pathToFile = 
        [[[NSBundle mainBundle] resourcePath] 
             stringByAppendingPathComponent:@"myFile.plist"];
    
    // or, even better (handling localization):
    NSString* pathToFile = [[NSBundle mainBundle] pathForResource:@"myFile"
                                                           ofType:@"plist"];
    

    【讨论】:

      【解决方案2】:

      您的应用文件夹是“主包”。所以可以使用 NSBundle 方法如

      NSString* path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 2011-06-05
        • 1970-01-01
        相关资源
        最近更新 更多