iOS避免文件被同步到iCloud或iTunes
在沙盒创建一个文件
- (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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。