与许多iOS开发人员一样,我在使用NSURLIsExcludedFromBackupKey
时遇到了5.1之前系统崩溃的问题。
如此评估如何在此线程上评估此密钥的存在:
Use NSURLIsExcludedFromBackupKey without crashing on iOS 5.0
samvermette's条评论之一表示iOS模拟器中存在错误。
尽管我在 发布 版本中遇到了同样的问题,即使是在2个独立的应用程序中也是如此。 经过一些调查后,我发现即使在main()方法调用之前应用程序崩溃了。这暗示我与
有关 NSString * const NSURLIsExcludedFromBackupKey;
我不是这个领域的专家,但我发现,如果代码中出现const
值的任何引用(即使它实际上没有在运行时访问),这个const
是在应用程序启动时评估。而这只会导致我们许多人经历的崩溃。
我想请你帮忙。也许你知道如何'弱'引用const值,或者可能有特定的编译器标志。 (使用Apple LLVM 3.1)。
提前致谢。
请不要在这种情况下直接输入这个const的值,即@“NSURLIsExcludedFromBackupKey”。我知道这个解决方法,这个故事的共鸣点是找到一个通用的解决方案。
答案 0 :(得分:1)
您可以在系统上使用此代码< 5.0.1
#include <sys/xattr.h>
- (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;
}
了解更多here。
修改强>
如果您只询问如何检查外部常量的可用性,可以将其地址与NULL或nil进行比较。这是recommended way的做法。
if (&NSURLIsExcludedFromBackupKey) {
// The const is available
}
答案 1 :(得分:1)
我找到了一个解决方案,感谢https://stackoverflow.com/a/9620714/127493!
NSString * const NSURLIsExcludedFromBackupKey;
即使Base SDK设置为iOS 5.1,也不是弱链接,与SDK Compatibility Guide所说的不同。
诀窍是使用这个const的结果 如果我做
NSLog(@"%@", NSURLIsExcludedFromBackupKey);
结果为@"NSURLIsExcludedFromBackupKey"
所以我的结果代码是
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
NSError * error = nil;
BOOL success;
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.1")) {
success = [storeURL setResourceValue:[NSNumber numberWithBool:YES] forKey:@"NSURLIsExcludedFromBackupKey" error:&error];
}