2012-08-08 50 views
3

我在NSDocumentDirectory中存儲一些下載的文件,我需要將這些文件標記爲「不備份」。哪個URL用於設置ios中的「不備份標誌」?

這裏是代碼我已經給

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL1 
    { 
     if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1 
     const char* filePath = [[URL1 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 
       NSError *error = nil; 
       [URL1 setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error]; 

       return error == nil; 
       NSLog(@"success"); 
      } 
     } 

littile有點困惑哪個URL應該的部份方法給出?下載url或下載的文件路徑url? 我可以使用像

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 


    NSString *documentsDirectoryPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"MdMus.mp3"]; 
    [receivedData writeToFile:documentsDirectoryPath atomically:YES]; 

需要使用一個

 NSURL *url=[NSURL fileURLWithPath:documentsDirectoryPath]; 
    [self addSkipBackupAttributeToItemAtURL:url]; 

或本

 [self addSkipBackupAttributeToItemAtURL:DownloadingURL]; 

需要幫助

+1

它應該是第一個'url',因爲它聲明文件不會通過iCloud從*文件夾*備份。我不能很好地回答這個問題,因爲我之前沒有使用過跳過備份。所以寫它作爲評論。但我很確定這是從documentdirectory獲得的網址。 – iNoob 2012-08-08 08:39:19

+0

definetly它應該是網址,因爲它聲明檢查[this](http://developer.apple.com/library/ios/#qa/qa1719/_index.html) – 2012-08-08 08:45:25

+0

@iNoob謝謝,讓我檢查 – 2012-08-08 09:27:59

回答

2

,如果你想要的文件共享的所有用戶數據(DocumentDirectory所有數據)阻止備份。參考下面的代碼。

- (void)addAttributeToAllFolder 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil]; 

    for (int i =0; i < [dirContents count]; i++) { 
     NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",documentsPath,[dirContents objectAtIndex:i]]]; 
     //this is your method (addSkipBackupAttributeToItemAtURL:) 
     if ([self addSkipBackupAttributeToItemAtURL:url]) { 
      NSLog(@"success! could add do not backup attribute to folder"); 
     } 
    } 
} 
+0

我的代碼設置我認爲每個文件的標誌?我對嗎? – 2012-08-08 09:27:11

+0

是的。最後,請使用NSLog檢查控制檯。您的表單的網址將如下所示。 (/var/mobile/Applications/AB36DF05-A13C-4DC4-8F29-E854050410C8/Documents/blahblah.mp3) – 2012-08-08 09:39:51