Prevent app from backing up documents folder?

后端 未结 2 1462
长发绾君心
长发绾君心 2020-12-25 08:59

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 chan

相关标签:
2条回答
  • 2020-12-25 09:45
    - (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];
    
    0 讨论(0)
  • 2020-12-25 09:46

    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
    }
    
    0 讨论(0)
提交回复
热议问题