首页 > 移动平台 > 详细

iOS避免文件被同步到iCloud或iTunes

时间:2015-03-26 13:09:30      阅读:248      评论:0      收藏:0      [点我收藏+]

在沙盒创建一个文件

 - (void)createSkipBackupImagesFolder {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/images"];

    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
        NSURL *toURL = [NSURL fileURLWithPath:dataPath];
        [self addSkipBackupAttributeToItemAtURL:toURL];
    }
}


避免该文件被同步到iCloud或iTunes,使用NSURLIsExcludedFromBackupKey    

- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
    {
    NSURL* URL= [NSURL fileURLWithPath: filePathString];
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
 
    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}

    

参考链接:https://developer.apple.com/library/ios/qa/qa1719/_index.html#//apple_ref/doc/uid/DTS40011342

http://stackoverflow.com/questions/12971192/how-should-i-prevent-files-from-being-backed-up-to-icloud-and-itunes-on-ios-5-0


iOS避免文件被同步到iCloud或iTunes

原文:http://my.oschina.net/leejan97/blog/391952

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!