问题
I am downloading many audio and video files and stored them in my Home directory. Now i want to "prevent backup to iCloud" so i have added following code for my every file's url
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
}
Can anyone tell me that will this code work for all IOS versions. if not then please suggest the correct way to do this. Thank You
回答1:
Can anyone tell me that will this code work for all IOS versions.
No, it doesn't. In its Technical Note introducing the "do not backup" flag, Apple clearly states that
The new "do not back up" attribute will only be used by iOS 5.0.1 or later.
They also tell you what you need to do for older iOS versions:
On iOS 5.0 and earlier, applications will need to store their data in
<Application_Home>/Library/Caches
to avoid having it backed up. Since this attribute is ignored on older systems, you will need to insure your app complies with the iOS Data Storage Guidelines on all versions of iOS that your application supports.
回答2:
You can use this code for iOS 5.1 or later
- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *)filePathString {
NSURL *fileURL = [NSURL fileURLWithPath:filePathString];
assert([[NSFileManager defaultManager] fileExistsAtPath: [fileURL path]]);
NSError *error = nil;
BOOL success = [fileURL setResourceValue:[NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey
error: &error];
return success;
}
来源:https://stackoverflow.com/questions/8558315/prevent-backup-to-icloud-is-following-code-correct