读书人

IOS-Do Not Backup属性

发布时间: 2012-07-25 09:43:05 作者: rapoo

IOS--Do Not Backup属性

首先贴一段官网的代码!~下面英文的文档没删除,大家可以对比看,思路虽然清晰,但是翻译水平有限,点到为止!

Listing 1 Excluding a File from Backups on iOS 5.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;
}

Listing 2 Setting the Extended Attribute 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;
}
It is not possible to exclude data from backups on iOS 5.0. If your app must support iOS 5.0, then you will need to store your app data in Caches to avoid that data being backed up. iOS will delete your files from theCaches directory when necessary, so your app will need to degrade gracefully if it's data files are deleted.

在IOS5.0中,不太可能取消data上传备份。如果你的app必须要支持IOS5.0,那么你需要将你的app数据存到Caches中防止上传备份。但是,在必要的时候,IOS会删掉caches目录。所以当你的data files被删除时,你的app要平稳的降级(最后一句没太看懂,知道大概意思)。

App Backup Best Practices

You do not have to prepare your app in any way for backup and restore operations. Devices with an active iCloud account have their app data backed up to iCloud at appropriate times. And for devices that are plugged into a computer, iTunes performs an incremental backup of the app’s data files. However, iCloud and iTunes do not back up the contents of the following directories:

简单的说上面那段话意思是说:你的app沙盒中的下面这些文件夹ICloud不会去备份!

读书人网 >操作系统

热点推荐