2012-10-17 20 views
31

Tôi đang cố gắng tải xuống tệp hình ảnh và lưu trữ trong NSDocumentDirectory. Để làm như vậy, tôi phải tắt sao lưu dữ liệu trên iCloud và iTunes. Dưới đây là mã của tôi:CFURLSetResourcePropertyForKey không thành công khi vô hiệu hóa sao lưu dữ liệu trên 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]; 
} 

và cho addskipbackupattributetoitematurl tôi:

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

Tuy nhiên, BOOL result = [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error]; tạo được thông báo sau

CFURLSetResourcePropertyForKey thất bại bởi vì nó đã được thông qua URL này mà không có kế hoạch : /var/mobile/Applications/CF69D567-1D37-4053-BFA8-5D0FCBD9C2B2/Documents/coffee.jpg

Tôi chỉ tự hỏi liệu có gặp phải vấn đề này không ??

+0

Hell với táo ... họ thậm chí không trả về một lỗi về vấn đề này và kết quả sẽ là YES. Nhưng điều này sẽ không đặt "ExcludedFromBackupKey". Họ từ chối tôi hai lần cho điều này: ( –

Trả lời

133

Đã giải quyết. một lần tôi đã thay đổi

NSURL *fileURL = [NSURL URLWithString:localFilePath]; 

để

NSURL *fileURL = [NSURL fileURLWithPath:localFilePath]; 

tất cả mọi thứ hoạt động hoàn hảo.

+0

đó là chìa khóa để chìa khóa! ;-) upvote! – rockstarberlin

+0

Điều đó đã giúp tôi tiết kiệm thêm hai giờ nữa! Cảm ơn bạn! – SexyBeast

+0

Bạn đã cứu mạng tôi! –

0

Swift:

let fileURL = URL(fileURLWithPath: somepath) 
Các vấn đề liên quan