如何阻止文件被iTunes和iCloud同步 How do I prevent files from being backed up to iCloud and iTunes?

http://blog.csdn.net/i2c_rs485/article/details/7386444


解决应用被打回的错误:

We found that your app does not follow the iOS Data Storage Guidelines, which is required per the App Store Review Guidelines.
In particular, we found that on launch and/or content download, your app stores 2.3MB. To check how much data your app is storing:




先阅读官方说明:

使用方法:
//iOS 5.1 and later
+ (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    if (![[NSFileManager defaultManager] fileExistsAtPath: [URL path]]) {
        NSLog(@"addSkipBackupAttributeToItemAtURL file not exist, URL = %@", URL);
        return NO;
    }
    
    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
#import "sys/xattr.h"
- (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;
}

- (void)addSkipBackupAttributeToPath:(NSString*)path {
    u_int8_t b = 1;
    setxattr([path fileSystemRepresentation], "com.apple.MobileBackup", &b, 1, 0, 0);
}

对文件夹的path使用这两个方法中的任意一个,就可以使该目录和该目录包含的所有文件和文件夹不被icloud和itunes同步了!
对于数据的同步与否的设计规则 详细看上面的官方说明链接。
注意:setxattr在iOS 5.0.1之前的系统里不会起作用的,(但是那些系统也没有iCloud只有iTunes^.^)。经测试发现虽然不会起作用,但调用该函数也不会引起空指针错误。  这是一项挺有意思的技术:新系统里加入了原先系统里没有的func,在原先的系统里调用,居然没有nullpoint错误。 莫非这函数地址早就存在,预留好了后面把功能挂上? 奇怪了。
另外补充一下,对这个特性的测试要有耐心,因为icloud识别应用程序里要同步的数据量大小显示要等几秒(菊花。。),
参考文档:apple官方文档,




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值