代码之家  ›  专栏  ›  技术社区  ›  mxg

如何访问iPhone应用程序内部的文件?

  •  3
  • mxg  · 技术社区  · 15 年前

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

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

    哪里 appFolder 是文件夹app的名称,如“test.app”。在知道appdir之后,访问文件很简单。

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

    事先谢谢!

    2 回复  |  直到 15 年前
        1
  •  4
  •   Nikolai Ruhe    15 年前
     NSString* pathToFile = 
        [[[NSBundle mainBundle] resourcePath] 
             stringByAppendingPathComponent:@"myFile.plist"];
    
    // or, even better (handling localization):
    NSString* pathToFile = [[NSBundle mainBundle] pathForResource:@"myFile"
                                                           ofType:@"plist"];
    
        2
  •  1
  •   kennytm    15 年前

    你的应用文件夹是“主包”。因此,您可以使用nsbundle方法,例如

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