Setting NSDocumentDirectory so it doesn't backup to iCloud

前端 未结 1 1399
离开以前
离开以前 2021-01-03 16:34

I\'m trying to mark the entire folder of my app\'s NSDocumentDirectory so that it is excluded from the iCloud backup, but when I go to the terminal and run: xattr -plxv com.

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-03 17:22

    How can you run Terminal on your iOS app's folder? You mean in the Simulator?

    For sure Apple is going to ignore or strip out that attribute from the primary Documents folder. What you should do is what Apple tells developers to do (from File Systems Programming Guide):

    Handle support files—files your application downloads or generates and can recreate as needed—in one of two ways:

    • In iOS 5.0 and earlier, put support files in the /Library/Caches directory to prevent them from being backed up

    • In iOS 5.0.1 and later, put support files in the /Library/Application Support directory and apply the com.apple.MobileBackup extended attribute to them. This attribute prevents the files from being backed up to iTunes or iCloud. If you have a large number of support files, you may store them in a custom subdirectory and apply the extended attribute to just the directory.

    So you create a new directory for your files inside Application Support, and apply the attribute to that directory.

    EDIT: Well, it seems that info is out of date and the document has not been updated. From the iOS 5.1 Release Notes:

    iOS 5.1 introduces a new API to mark files or directories that should not be backed up. For NSURL objects, add the NSURLIsExcludedFromBackupKey attribute to prevent the corresponding file from being backed up. For CFURLRef objects, use the corresponding kCFURLIsExcludedFromBackupKey attribute.

    Apps running on iOS 5.1 and later must use the newer attributes and not add the com.apple.MobileBackup extended attribute directly, as previously documented. The com.apple.MobileBackup extended attribute is deprecated and support for it may be removed in a future release.

    It turns out that you can get actual code to do this in the Technical Q&A QA1719.

    Also, I found I need to create the Application Support directory, at least in the Simulator. Hope this code helps:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // got to make sure this exists
        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *appSupportDir = [self applicationAppSupportDirectory];
        if(![manager fileExistsAtPath:appSupportDir]) {
            __autoreleasing NSError *error;
            BOOL ret = [manager createDirectoryAtPath:appSupportDir withIntermediateDirectories:NO attributes:nil error:&error];
            if(!ret) {
                LTLog(@"ERROR app support: %@", error);
                exit(0);
            }
        }
        ...
    }
    
    - (NSString *)applicationAppSupportDirectory
    {
        return [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject];
    }
    

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