NSURLIsExcludedFromBackupKey crashes before iOS 5.1

孤者浪人 提交于 2019-12-05 14:24:26

You can use this code on systems < 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;
}

Read more here.

edit

If you're only asking how to check for availability of an external constant you can compare its address to NULL or nil. This is the recommended way of doing it.

if (&NSURLIsExcludedFromBackupKey) {
    // The const is available
}
Martin

I found a solution, thanks to https://stackoverflow.com/a/9620714/127493 !

NSString * const NSURLIsExcludedFromBackupKey;

is NOT weak-linked, even if Base SDK is set to iOS 5.1, unlike the SDK Compatibility Guide says.

The trick is to use the result of this const.
If I do

NSLog(@"%@", NSURLIsExcludedFromBackupKey);

the result is @"NSURLIsExcludedFromBackupKey"

So my resulting code is

#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];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!