3

我正在下载许多音频和视频文件并将它们存储在我的主目录中。现在我想“防止备份到 iCloud”,所以我为每个文件的 url 添加了以下代码

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

}

谁能告诉我此代码适用于所有 IOS 版本。如果没有,请提出正确的方法来做到这一点。谢谢你

4

2 回答 2

7

谁能告诉我此代码适用于所有 IOS 版本。

不,它没有。在其介绍“不备份”标志的技术说明中,Apple 明确指出

新的“不备份”属性仅适用于 iOS 5.0.1 或更高版本。

他们还告诉您对于旧 iOS 版本需要做什么:

在 iOS 5.0 及更早版本上,应用程序需要将其数据存储在<Application_Home>/Library/Caches其中以避免备份。由于在旧系统上会忽略此属性,因此您需要确保您的应用程序在您的应用程序支持的所有 iOS 版本上都符合 iOS 数据存储指南。

于 2011-12-19T11:28:45.410 回答
2

您可以将此代码用于 iOS 5.1 或更高版本

- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *)filePathString {
    NSURL *fileURL = [NSURL fileURLWithPath:filePathString];

    assert([[NSFileManager defaultManager] fileExistsAtPath: [fileURL path]]);

    NSError *error = nil;

    BOOL success = [fileURL setResourceValue:[NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey
                                 error: &error];
    return success;
}
于 2013-07-18T12:23:33.817 回答