您的位置:首页 > 其它

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

2015-01-28 14:08 701 查看
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:



先阅读官方说明:

https://developer.apple.com/library/ios/#qa/qa1719/_index.html

使用方法:

//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官方文档,

stackoverflow问答:http://stackoverflow.com/questions/8694112/adding-the-do-not-backup-attribute-to-a-folder-hierarchy-in-ios-5-0-1

原文链接:http://chishangjin.sinaapp.com/?p=14
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐