Use NSURLIsExcludedFromBackupKey without crashing on iOS 5.0

#include <sys/xattr.h>
-(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{

constchar* filePath =[[URL path] fileSystemRepresentation];
constchar* 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
   
NSError*error =nil;
   
[URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
   
return error ==nil;
}
}
posted @ 2012-05-04 09:33  harvey.ding  阅读(759)  评论(0)    收藏  举报