2012-12-03 56 views
4

我試圖阻止我的應用程序將文件備份到iCloud,但已經變得完全困惑和有點失落。防止應用程序備份文檔文件夾?

CNC中

我已經更新了這個以反映我做了感謝以下海報的變化。

我想阻止備份下載到應用程序文檔目錄的文件。

到目前爲止,我有一個用下面的方法稱爲PreventBackup類:

+ (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL 
{  
    NSError *error = nil; 
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES] 
           forKey: NSURLIsExcludedFromBackupKey error: &error]; 
    if(!success){ 
     NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error); 
    } 
    NSLog(@"prevent backup method called without error"); 
    return success; 
} 

我然後用這個代碼調用它時,應用程序啓動時:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSURL *pathURL= [NSURL fileURLWithPath:documentsDirectory]; 
[PreventBackup addSkipBackupAttributeToItemAtURL:pathURL]; 

的cosole打印prevent backup method called without error但該應用程序仍然顯示與之前的備份數據量相同。

任何想法哪裏出錯了?

-EDIT 2-

好的,我認爲這是解決的。這些文件正在下載到一個名爲「downloads」的子文件夾中。改變上面的代碼,以便它讀取如下,似乎已經做了訣竅:

NSString *downloadsFolder = [documentsDirectory stringByAppendingPathComponent:(@"/downloads")]; 
NSURL *pathURL= [NSURL fileURLWithPath:downloadsFolder]; 
[PreventBackup addSkipBackupAttributeToItemAtURL:pathURL]; 

感謝大家的幫助。

+1

檢查:HTTP://計算器。 COM /問題/ 108 66590/prevent-icloud-backup-of-folder?lq = 1和http://stackoverflow.com/questions/8092256/backing-up-prevent-from-the-app-in-icloud?rq=1 –

+0

謝謝。我已經看到了第二個鏈接,但它沒有幫助,但第一個鏈接。 – Robert

回答

6
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL 
{ 

    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]); 

    NSError *error = nil; 
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES] 
            forKey: NSURLIsExcludedFromBackupKey error: &error]; 
    if(!success){ 
     NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error); 
    } 
    return success; 
} 

NSURL *documentURLWithExtension = [documentURL URLByAppendingPathExtension:extensionType]; 

通這個 「documentURLWithExtension」 這個功能

[self addSkipBackupAttributeToItemAtURL:documentURLWithExtension]; 
+0

非常感謝。這似乎只是一個小小的提示而已。我需要註釋掉'assert([[NSFileManager defaultManager] fileExistsAtPath:[URL path]]);' – Robert

6

在斯威夫特:

//Path of document directory 
var docPathAry : NSArray! = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
var docDirPathStr: AnyObject? = docPathAry.count > 0 ? docPathAry[0] : nil 

self.addSkipBackupAttributeToItemAtURL(NSURL.fileURLWithPath(docDirPathStr as NSString)) 

和:

func addSkipBackupAttributeToItemAtURL(URL: NSURL!) -> Bool { 
    assert(NSFileManager.defaultManager().fileExistsAtPath(URL.path)) 
    var err : NSError? = nil 
    var success : Bool! = URL.setResourceValue(NSNumber.numberWithBool(true), forKey: NSURLIsExcludedFromBackupKey, error: &err) 

    if(!success) { 
     println("Error excluding \(URL.lastPathComponent) from backup\(err) ") 
    } 

    return success 
} 
相關問題