Setting NSDocumentDirectory so it doesn't backup to iCloud

半腔热情 提交于 2019-11-30 16:32:56
David H

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 <Application_Home>/Library/Caches directory to prevent them from being backed up

  • In iOS 5.0.1 and later, put support files in the <Application_Home>/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];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!