加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 附加您的目录.它是什么?

发布时间:2020-12-14 17:32:36 所属栏目:百科 来源:网络整理
导读:对于iOS 7,我在将文件写入 Xcode 5中的AppSupport文件夹时遇到了麻烦. 我正在尝试做什么: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,NSUserDomainMask,YES);NSString *plistPath = [[paths lastObject] stringB
对于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);
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读