2012-10-17 9 views
31

Próbuję pobrać pliki obrazów i przechowywać je w NSDocumentDirectory. W tym celu muszę wyłączyć tworzenie kopii zapasowych danych w iCloud i iTunes. Poniżej są moje kody:CFURLSetResourcePropertyForKey nie powiodło się, gdy wyłączono tworzenie kopii zapasowej danych w katalogu NSDocumentDirectory

+(void)saveData:(NSData*)thedata:(NSString*)fileName 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:fileName]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    [fileManager createFileAtPath:localFilePath contents:thedata attributes:nil]; 
    //prevent files from backup on iCloud or iTune 
    NSURL *fileURL = [NSURL URLWithString:localFilePath]; 
    [self addSkipBackupAttributeToItemAtURL:fileURL]; 
} 

i dla mojego addskipbackupattributetoitematurl:

+(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)fileURL 
{ 
    if (![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) 
    { 
     NSLog(@"File %@ doesn't exist!",[fileURL path]); 
     return NO; 
    } 
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];  
    if ([currSysVer isEqualToString:@"5.0.1"]) 
    { 
     const char* filePath = [[fileURL path] fileSystemRepresentation]; 
     const char* attrName = "com.apple.MobileBackup"; 
     u_int8_t attrValue = 1; 
     int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0); 
     NSLog(@"Excluded '%@' from backup",fileURL); 
     return result == 0; 
    } 
    else if (&NSURLIsExcludedFromBackupKey) 
    { 
     NSError *error = nil; 
     BOOL result = [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error]; 
     if (result == NO) 
     { 
      NSLog(@"Error excluding '%@' from backup. Error: %@",fileURL, error); 
      return NO; 
     } 
     else 
     { 
      NSLog(@"Excluded '%@' from backup",fileURL); 
      return YES; 
     } 
    } 
    else 
    { 
     return YES; 
    } 
} 

Jednak BOOL result = [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error]; tworzony następujący komunikat

CFURLSetResourcePropertyForKey powiodło się, ponieważ została podjęta ten adres URL, który nie ma schematu : /var/mobile/Applications/CF69D567-1D37-4053-BFA8-5D0FCBD9C2B2/Documents/coffee.jpg

Zastanawiam się, czy ktoś napotkał ten problem?

+0

Piekło z jabłkiem ... nawet nie zwróci błąd w tej sprawie, a wynik będzie TAK. Ale to nie ustawi "ExcludedFromBackupKey". Odrzucili mnie dwa razy za to :( –

Odpowiedz

133

Rozwiązany. raz zmieniłem

NSURL *fileURL = [NSURL URLWithString:localFilePath]; 

do

NSURL *fileURL = [NSURL fileURLWithPath:localFilePath]; 

wszystko działa idealnie.

+0

, który był kluczem do klucza! ;-) upvote! – rockstarberlin

+0

To uratowało mnie przed dwiema godzinami szumowania! Dziękuję Ci! – SexyBeast

+0

Uratowałeś mi życie! –

0

Swift:

let fileURL = URL(fileURLWithPath: somepath) 
Powiązane problemy