无法在ios8中的文档目录中复制可写plist文件

时间:2014-11-06 04:45:31

标签: ios ios8

我试图复制一个plist文件,其中有一个http url从app bundle写到文档目录但是它只在ios8中失败,消息为“NSInternalInconsistencyException”,原因是:'操作无法完成。(Cocoa)错误4.)'。'“。它在ios7中正常工作。请指导原因。

//To get the plist file path within app
-(NSString *)ogPath
{
    NSString *str = [[NSBundle mainBundle] bundlePath];
    str = [str stringByAppendingPathComponent:@"configUrl.plist"];
    return str;
}
//To get the plist file path within documents directory
-(NSString *)documentPath
{
    NSString *str = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
    str = [str stringByAppendingFormat:@"/Documents/configUrl.plist"];
    return str;
}
//to copy plist file within app to documents directory
-(void)copytheplistfile
{
    NSString *plistpath = [self ogPath];
    NSString *docpath = [self documentPath];
    NSFileManager *file = [NSFileManager defaultManager];
    BOOL fileexist = [file fileExistsAtPath:docpath];
    if(!fileexist)
    {
        NSError *err;
        BOOL copyResult=[file copyItemAtPath:plistpath toPath:docpath error:&err];
        NSLog(@"error:%@",[err localizedDescription]);
        if(!copyResult)
            NSAssert1(0, @"Failed to create writable plist file with message '%@'.", [err localizedDescription]);
    }
}

1 个答案:

答案 0 :(得分:0)

在iOS 8中,应用程序容器的文件系统布局已更改。应用程序及其内容不再存储在一个根目录中。

更改文档路径

 -(NSString *)documentPath
{
    NSString *str=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    str = [str stringByAppendingFormat:@"/configUrl.plist"];

    return str;
}