在iOS 5.0上使用NSURLIsExcludedFromBackupKey不会崩溃

检查可用性似乎工作正常,但我似乎无法设置NSURLIsExcludedFromBackupKey键没有得到这个崩溃发射:

dyld:找不到符号:_NSURLIsExcludedFromBackupKey引用自:/ Users / sam / Library / Application Support / iPhone Simulator / 5.0 / Applications / B0872A19-3230-481C-B5CE-D4BDE264FBDF / Transit.app / Transit预计位于:/ Applications / Xcode。应用程序/目录/开发人员/平台/ iPhoneSimulator.platform /开发人员/软件开发工具包/ iPhoneSimulator5.0.sdk /系统/库/框架/ Foundation.framework /基础在/用户/ SAM /库/应用程序支持/ iPhone模拟器/ 5.0 /应用程序/B0872A19-3230-481C-B5CE-D4BDE264FBDF/Transit.app/Transit

这是我的方法:

 - (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL { if (&NSURLIsExcludedFromBackupKey == nil) return NO; NSError *error; [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error]; return (error != nil); } 

如果我注释掉这一行,崩溃就会消失:

 [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error]; 

我必须弱链接基础?

编辑:不知道是否NSFileManager ,但这种方法是放在一个NSFileManager类别。

这是iOS的代码<= 5.0.1和> = 5.1,包括@Cocoanetics提到的迁移技术。

 - (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL { const char* filePath = [[URL path] fileSystemRepresentation]; const char* attrName = "com.apple.MobileBackup"; if (&NSURLIsExcludedFromBackupKey == nil) { // iOS 5.0.1 and lower u_int8_t attrValue = 1; int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0); return result == 0; } else { // First try and remove the extended attribute if it is present int result = getxattr(filePath, attrName, NULL, sizeof(u_int8_t), 0, 0); if (result != -1) { // The attribute exists, we need to remove it int removeResult = removexattr(filePath, attrName, 0); if (removeResult == 0) { NSLog(@"Removed extended attribute on file %@", URL); } } // Set the new key return [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil]; } } 

这似乎是iPhone 5.0模拟器的一个错误。 我试着在5.0设备上运行代码,没有崩溃。 报告这个错误为rdar:// 11017158 。

编辑 :这已经修复在Xcode 4.5 DP2(不知道是否在4.4)。

添加此行以强制该符号成为弱导入:

 extern NSString * const NSURLIsExcludedFromBackupKey __attribute__((weak_import)); 

根据你在做什么,这个快速修复可能会为你工作。 它为我做了。

CoreFoundation框架添加到您的项目并将其标记为可选 (不需要)。

问题是这个密钥只存在于5.1及以上。 对于5.0.1,您需要设置扩展文件属性。 唯一向后兼容的方法是找出这个键的NSString值,并把它设置在5.1以下。

我更新了ShareKit和重做一个项目,目标iOS 5.1后,我有这个相同的东西我会得到一个错误在编译或链接相关的NSURLIsExcludedFromBackupKey。 ShareKit的人似乎build议您可以通过确保您的项目链接到CoreFoundation框架并将其设置为“可选”而不是“必需”来解决问题。 然而,这并没有为我工作。

最终我通过使用预处理器来解决它:

 - (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL { #ifndef NSURLIsExcludedFromBackupKey // iOS <= 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 // iOS >= 5.1 // First try and remove the extended attribute if it is present int result = getxattr(filePath, attrName, NULL, sizeof(u_int8_t), 0, 0); if (result != -1) { // The attribute exists, we need to remove it int removeResult = removexattr(filePath, attrName, 0); if (removeResult == 0) { NSLog(@"Removed extended attribute on file %@", URL); } } return [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil]; #endif }