iOS: Do not back up attribute?

后端 未结 3 1628
难免孤独
难免孤独 2020-12-15 05:20

Our app has recently been rejected for violating iOS Data Storage Guidelines about backing up files on iCloud.

I must mark data with the do not ba

相关标签:
3条回答
  • 2020-12-15 05:58

    Swift 5 version:

    func addSkipBackupAttributeTo(url constURL: URL) {
        do {
            // Need a mutable URL to call setResourceValues
            var url = constURL
            var resourceValues = URLResourceValues()
            resourceValues.isExcludedFromBackup = true
            try url.setResourceValues(resourceValues)
        } catch {
            print("Unable to set isExcludedFromBackup on \(constURL)")
        }
    }
    

    URLResourceValues keeps track of which values are modified and only writes those out, so it's fine to initialize a new object, set one key and call setResourceValues with it.

    0 讨论(0)
  • 2020-12-15 06:02

    You can call the following function to pass the path of your "map.sqlite" as NSURL:

    NSURL *url = [NSURL fileURLWithPath:yourSQLitePath];
    [self addSkipBackupAttributeToItemAtURL:url];
    The function is provided in Apple as:
    
    - (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;
    }
    
    0 讨论(0)
  • 2020-12-15 06:17

    Basically, it depends on the kind of data you are storing.

    If the data can be regenerated (when a user install the app on another device, for instance), then it should not be backed up.

    Otherwise, iCloud backup is OK, as the user will expect his data to be available, even on another device.

    In the first scenario, you have basically two ways of achieving this...

    Either you use NSURL to set the kCFURLIsExcludedFromBackupKey on your files, either you store them in a location that won't be backed-up, like <Application_Home>/Library/Caches. Note that the second solution is the better, IMHO.

    For info, kCFURLIsExcludedFromBackupKey can be used this way:

    NSURL * fileURL;
    
    fileURL = [ NSURL fileURLWithPath: @"some/file/path" ];
    
    [ fileURL setResourceValue: [ NSNumber numberWithBool: YES ] forKey: NSURLIsExcludedFromBackupKey error: nil ];
    

    For the second scenario, sometimes Apple reviewers think your data can be re-generated, when it's not. Then you'll have to explain why the data has to be backed-up.

    0 讨论(0)
提交回复
热议问题