How can I prevent files from being backed up to iCloud and iTunes?

Source: Internet
Author: User
Tags file url

Some files of the application need to be permanently stored locally so that the application supports offline functions. However, these files do not contain user data and do not need to be backed up. How to prevent these files from being backed up.

On iOS, the app is responsible for ensuring that only user data, not application data, is backed up to iCloud and iTunes. The specific steps are different in different iOS versions. Different versions are described separately. For details about which data should not be backed up, see App Backup Best Practices section of the iOS App Programming Guide.

 

Note: do not include application data and user data in the same file. This will increase the unnecessary backup size and is considered a violation of the iOS data storage guide.

 

IOS 5.1 and updates

 

From iOS 5.1, applications can use the NSURLIsExcludedFromBackupKey or kCFURLIsExcludedFromBackupKey file attribute to prevent files from being backed up. These APIs directly set additional attributes through the old and obsolete method. All operations running on iOS5.1 should use these API packages to prevent files from being backed up.

Prevent files from being backed up on iOS5. 1

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    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;
}

 

IOS 5.0.1

If your app must support iOS 5.0.1, you can use the following method to set the extended attribute of "do not back up. When you create a file or folder that does not need to be backed up, write the data to the file and call this method to input a File URL.

 

The following code has been discarded and should only be used in iOS5.0.1 and earlier versions. When iOS5.1 is running, the application uses the NSURL and CFURL keys for description.

Set additional attributes on iOS 5.0.1

#import <sys/xattr.h>
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
 
    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;
}

 

For iOS 5.0

Data cannot be backed up on iOS5.0. If your application must support iOS5.0, you need to keep your application data in Caches to avoid data backup. IOS will delete the data in the Caches directory when it is not needed. Therefore, if the data is deleted, the application will need to decompress the data again.

This article is from the "arsurchen" blog, please be sure to keep this source http://arthurchen.blog.51cto.com/2483760/1125762

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.