Use NSURLIsExcludedFromBackupKey without crashing on iOS 5.0

前端 未结 6 1272
我在风中等你
我在风中等你 2020-12-02 15:55

The check for availability seems to be working fine but I can\'t seem to set the NSURLIsExcludedFromBackupKey key without getting this crash on launch:

6条回答
  •  庸人自扰
    2020-12-02 16:58

    I had this same thing after updating ShareKit and redoing a project to target iOS 5.1 I would either get an error at compilation or at linking related to NSURLIsExcludedFromBackupKey. The ShareKit people seem to recommend that you can solve the problem by making sure your project links with the CoreFoundation framework and set it to "Optional" rather than "Required". However this didn't work for me.

    Eventually i got around it by using the preprocessor:

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

提交回复
热议问题