使用iCloud和itunes阻止数据备份

时间:2012-08-09 13:43:37

标签: objective-c ios icloud

我正在开发一个iOS应用程序。用户可以下载书籍,书籍的大小ar> 200 Mo.为了尊重苹果存储指南,我们这样做:

 + (NSString *)booksStoragePath
    {

        NSArray *path = [NSArray array];
        NSString *os5 = @"5.0";
        NSString *currSysVer = [[UIDevice currentDevice] systemVersion];

        if ([currSysVer compare:os5 options:NSNumericSearch] == NSOrderedDescending) //5.0.1 and above
        {        
            path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

        }
        else // IOS 5
        {
            path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
        }

       NSString *documentsDirectory = [path objectAtIndex:0]; // Get documents directory
        NSString *storagePath = [documentsDirectory stringByAppendingPathComponent:@"Books"];

        if (![[NSFileManager defaultManager] fileExistsAtPath:storagePath])
            [[NSFileManager defaultManager] createDirectoryAtPath:storagePath withIntermediateDirectories:YES attributes:nil error:nil];

NSUrl *url = [NSUrl urlWithString:storagePath];
[self addSkipBackupAttributeToItemAtURL:url];

        return storagePath;
    }

为了不备份“Books”目录,我这样做:

#pragma mark --
#pragma mark Data Backup
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    NSString *os5 = @"5.0.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];


    if ([currSysVer compare:os5 options:NSNumericSearch] == NSOrderedSame) //5.0.1
    {    
        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;

    }
    else{
        if ([currSysVer compare:os5 options:NSNumericSearch] == NSOrderedDescending) {



            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;
        }
    }

    return nil;

}

问题是我看到iTunes中的“Books”文件夹连接我可以下载它,所有书籍的内容都在那里!!!!!

我怎么能禁止目录“书籍”及其内容与iCloud不同步而不会出现在iTunes中?

感谢您的回答。

0 个答案:

没有答案