Disable backup of documents folder and all its sub folders on icloud?

后端 未结 4 984
离开以前
离开以前 2020-12-19 09:55

I have an already running enterprise application which stores a lot of documents inside the documents folders. I wish to disable the iCloud backup of all these documents and

相关标签:
4条回答
  • 2020-12-19 10:28

    In appDelegate of your application in the following method

     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    

    Add this code

        NSArray *urlArray = [[NSFileManager defaultManager] URLsForDirectory: NSDocumentDirectory inDomains: NSUserDomainMask];
        NSURL *documentsUrl = [urlArray firstObject];
    
        NSError *error = nil;
        BOOL success = [documentsUrl setResourceValue: [NSNumber numberWithBool: YES]
                                      forKey: NSURLIsExcludedFromBackupKey error: &error];
        if(!success){
            NSLog(@"Error in disabling %@ from backup %@", [documentsUrl lastPathComponent], error);
        }
    
    0 讨论(0)
  • 2020-12-19 10:42
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *directory = paths[0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *url = [NSURL fileURLWithPath:directory];
    
    NSError *error;
    // exclude from iCloud backup
    if ([url setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:&error] == NO) {
            NSLog(@"Error: Unable to exclude directory from backup: %@", error);
    }
    

    You can call these once at first install. And it will apply on subfolders and files.

    0 讨论(0)
  • 2020-12-19 10:43

    Perhaps the files are changing dynamically because they are being regularly updated from a server or similar? If so, then the answer is to move your file storage from the <Application_Home>/Documents directory to the <Application_Home>/Library/Caches directory. These documents will not be backed up. This is of course only suitable if the data can be re-downloaded or recreated in the future, if required.

    See this Apple documentation for an overview: https://developer.apple.com/icloud/documentation/data-storage/index.html

    0 讨论(0)
  • 2020-12-19 10:49

    You can put kCFURLIsExcludedFromBackupKey (or NSURLIsExcludedFromBackupKey) to the entire Documents directory just like you would for a single file. However, if you don't want any of the files backed up, you may want to rethink where they should go. The Documents directory is for user data only. If files are stored on your own servers, they should probably go in Caches because they can be downloaded again easily. If the files are only needed for a short period of time, they should go in tmp/.

    This is Apple's documentation about where to put your app's files.

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