简体   繁体   中英

Fails to copy writable plist file in document directory in ios8

Am trying to copy a plist file which has a http url written to it from app bundle to documents directory but its getting failed only in ios8 with message "NSInternalInconsistencyException', reason: 'The operation couldn't be completed. (Cocoa error 4.)'.'" .Its working fine in ios7.Please guide what could be the reason.

//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]);
    }
}

In iOS 8 file system layout of app containers has been changed. Applications and their content are no longer stored in one root directory.

Change your document path

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

    return str;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM