Prevent Backup to iCloud,is following code correct?

核能气质少年 提交于 2020-01-01 14:24:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!