1

我正在开发一个 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 中看到文件夹“书籍”连接,我可以下载它,书籍的所有内容都在那里!!!!!!

我如何禁止目录“书籍”及其内容与 iCloud 不同步,并且不会出现在 iTunes 中?

感谢您的回答。

4

0 回答 0