Prevent app from backing up documents folder?

筅森魡賤 提交于 2019-12-18 10:57:44

问题


I'm trying to prevent my app backing up files to iCloud but have become completely confused and a little lost.

-EDIT-

I've updated this to reflect the changes I've made thanks to the posters below.

I want to prevent back up of files which are downloaded to the app's documents directory.

So far I have a class called PreventBackup with the following method:

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

I'm then calling it with this code when the app starts:

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

The cosole prints prevent backup method called without error but the app is still showing as having the same amount of data for back up with it did before.

Any idea where this is going wrong?

-EDIT 2-

OK, I think this is solved. The files are downloading to a sub folder called "downloads". changing the code above so that it reads as follows appears to have done the trick:

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

Thanks to all for your help.


回答1:


- (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];

pass this "documentURLWithExtension" to this function

[self addSkipBackupAttributeToItemAtURL:documentURLWithExtension];



回答2:


In Swift:

//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))

and:

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
}


来源:https://stackoverflow.com/questions/13682859/prevent-app-from-backing-up-documents-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!