ios - 附加您的 <bundle_ID> 目录。它是什么?

标签 ios xcode plist

我在为 iOS 7 的 Xcode 5 中将文件写入 AppSupport 文件夹时遇到问题。 我正在尝试做的事情:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[paths lastObject] stringByAppendingPathComponent:@“somedata.plist”];
if(plistData) {
    NSLog(@"PATH: %@", plistPath);
    BOOL success = [plistData writeToFile:plistPath atomically:YES];
    NSLog(@"SUCCESS: %hhd",success);
}
else {
    NSLog(@"ERROR CREATING PLIST: %@",error);
}

我得到的输出是 NO:

PATH: /var/mobile/Applications/40954....7E3C/Library/Application Support/somedata.plist
SUCCESS: 0

Apple 文档说:

Use the Application Support directory constant NSApplicationSupportDirectory, 
appending your <bundle_ID> for: …

附加您的 bundle_ID 是什么意思?可能有我应该使用的另一条路径吗? NSDocumentDirectory 不适合我,因为它是用户文件的地方。

最佳答案

存储到 NSApplicationSupportDirectory 比这复杂一点,

关注这个Apple sample code将文件写入此目录,

- (NSURL*)applicationDirectory
{
    NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
    NSFileManager*fm = [NSFileManager defaultManager];
    NSURL* dirPath = nil;

    // Find the application support directory in the home directory.
    NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
                                        inDomains:NSUserDomainMask];
    if ([appSupportDir count] > 0)
    {
        // Append the bundle ID to the URL for the
        // Application Support directory
        dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];

        //Modified code to write your plist file to the Application support dir
        NSString *plistPath = [dirPath stringByAppendingPathComponent:@“somedata.plist”];

        //Assuming plistData is pre-populated ivar
        //Else add your code to create plistData here 
        if(plistData) {
            BOOL success = [plistData writeToFile:plistPath atomically:YES];
            NSLog(@"SUCCESS: %hhd",success);
        }
        else {
            NSLog(@"ERROR CREATING PLIST: %@",error);
        }
    }
}

关于ios - 附加您的 <bundle_ID> 目录。它是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22015014/

相关文章:

objective-c - 私有(private)枚举声明

macos - PlistBuddy 默默地失败了——bug?

cocoa - 将数据从 Plist 加载到数组时遇到问题

objective-c - 如何在从目标 View Controller 返回之前执行操作

ios - 为一年中的每一天生成 View Controller

ios - 如何编辑 UIScrollView 大小?

iphone - 将图像保存在NSUserDefault中并获取其他ViewController

iOS - 如何将参数从 didFinishLaunchingWithOptions 传递到 viewController?

ios - 如何让我所有的自动布局约束自行更新? (例如,当用户更新他们的系统大小/动态类型时)?

iphone - 我想在 iTunes Connect 中将我的通用应用程序更新为仅限 iPad 的应用程序。但是在提交二进制文件时出现以下错误