没有安装Lion支持iOS 5.1 NSURLIsExcludedFromBackupKey [不备份]?

时间:2012-05-27 12:46:29

标签: ios ios5

我发现显然可以submit apps without Lion installed,但我的应用会下载一些大文件,需要根据Apple storage guidelines进行保存。

包括下面的代码,由于Use of undeclared identifier 'NSURLIsExcludedFromBackupKey'

,我甚至无法编译
-(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.1")) {
        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;
    }
}

如果我只在xcode中使用iOS 5.0 SDK,我该如何继续使用此代码?

(或者我的愿望没有实际意义,因为我实际上无法告诉我的应用它支持iOS 5.1?)

1 个答案:

答案 0 :(得分:0)

如果ios版本不是5.1,我添加了该字符串常量的定义。

以下是我的实现方式:

+ (BOOL)addSkipBackupAttributeToItemAtURL:(NSString*) path
    {           
        if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"5.01"))
        {
            const char* folderPath = [_privateDirectoryPath fileSystemRepresentation];
            u_int8_t attrValue = 1;
            int result = setxattr(folderPath, kMobileBackupAttrName, &attrValue, sizeof(attrValue), 0, 0);
            return result == 0;
        } 
        else
        {
    #ifndef __IPHONE_5_1
    #define NSURLIsExcludedFromBackupKey @"NSURLIsExcludedFromBackupKey"
    #endif
            NSError *error = nil;
            NSURL* url = [NSURL fileURLWithPath:path];
            ASSERT_CLASS(url, NSURL);
            BOOL success = [url setResourceValue: [NSNumber numberWithBool: YES]
                                          forKey: NSURLIsExcludedFromBackupKey 
                                           error: &error];
            //Assert success                
            return success;
        }
    }