设置NSDocumentDirectory,使其不备份到iCloud [英] Setting NSDocumentDirectory so it doesn't backup to iCloud

查看:434
本文介绍了设置NSDocumentDirectory,使其不备份到iCloud的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试标记我的应用程序的NSDocumentDirectory的整个文件夹,以便将其从iCloud备份中排除,但是当我转到终端并运行:xattr -plxv com.apple.MobileBackup时出现此错误:否这样的xattr:com.apple.MobileBackup

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.apple.MobileBackup I get this error: No such xattr: com.apple.MobileBackup

提前感谢您提供任何帮助。

Thank you in advance for any help offered.

这是我正在使用的代码:

Here is the code I'm using:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSURL *pathURL= [NSURL fileURLWithPath:documentsDirectory];

    [self addSkipBackupAttributeToItemAtURL:pathURL];
}

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1
        const char* filePath = [[URL path] fileSystemRepresentation];

        const char* attrName = "com.apple.MobileBackup";
        u_int8_t attrValue = 1;

        int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
        return result == 0;
    } else { // iOS >= 5.1

        NSLog(@"%d",[URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil]);
        return [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil];
    }
}


推荐答案

如何你可以在iOS应用程序的文件夹上运行终端吗?你的意思是在模拟器中?

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

确定Apple会忽略或从主Documents文件夹中删除该属性。您应该做的是Apple告诉开发人员要做的事情(来自文件系统编程指南):

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:


  • 在iOS 5.0及更早版本中,将支持文件放在< Application_Home>中。 / Library / Caches 目录,以防止它们被备份

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

在iOS 5.0中.1及更高版本,将支持文件放在< Application_Home> / Library / Application Support 目录中,并应用com.apple.MobileBackup扩展属性给他们。此属性可防止将文件备份到iTunes或iCloud。如果您有大量支持文件,则可以将它们存储在自定义子目录中,并将扩展属性应用于该目录。

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.

因此,您在 Application Support 中为您的文件创建一个新目录,并将该属性应用于该目录。

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

编辑:好吧,似乎信息已过期且文档尚未更新。来自 iOS 5.1发行说明

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引入了一个新的API,用于标记不应备份
的文件或目录。对于 NSURL 对象,添加
NSURLIsExcludedFromBackupKey 属性以防止相应的
文件被备份向上。对于 CFURLRef对象,请使用相应的
kCFURLIsExcludedFromBackupKey 属性。

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.

在iOS 5.1和
之后运行的应用必须使用较新的属性,而不是直接添加
com.apple.MobileBackup 扩展属性,如前所述
记录。 com.apple.MobileBackup 扩展属性已弃用
,并且可能会在将来的版本中删除对它的支持。

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.

事实证明,您可以在技术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];
}

这篇关于设置NSDocumentDirectory,使其不备份到iCloud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆