App程序会同步程序到iCloud或iTunes,为了数据安全,可以通过代码避免指定文件被同步到苹果服务器
在沙盒创建一个文件
1
2
3
4
5
6
7
8
9
10
11
12
|
- ( 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
1
2
3
4
5
6
7
8
9
10
11
12
13
|
- ( 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