Prevent Backup to iCloud,is following code correct

2019-04-12 05:04发布

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

2条回答
我想做一个坏孩纸
2楼-- · 2019-04-12 05:16

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.

查看更多
一夜七次
3楼-- · 2019-04-12 05:16

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;
}
查看更多
登录 后发表回答