【问题标题】:NSURLIsExcludedFromBackupKey does not work on iOS 8NSURLIsExcludedFromBackupKey 在 iOS 8 上不起作用
【发布时间】:2015-05-20 12:02:48
【问题描述】:

我使用标准方法从备份中排除:

+ (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath:[URL path]]);

    NSError *error = nil;
    BOOL success = [URL setResourceValue:@(YES)
                                  forKey:NSURLIsExcludedFromBackupKey error:&error];
    if (!success) {
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }

    return success;
} 

正如一些人建议的那样,我将此方法与 [NSURL fileURLWithPath:path] 一起使用,但它不起作用:

从备份中排除 groups.xml 时出错 Error Domain=NSCocoaErrorDomain Code=513 "Не удалось завершить операцию. (Cocoa, ошибка 513)"

用户信息=0x170272ac0 {

NSURL=file:///private/var/mobile/Containers/Bundle/Application/59286088-C226-4653-A84F-A4B5D40C11DE/anatomyfree.app/groups.xml, NSFilePath=/private/var/ mobile/Containers/Bundle/Application/59286088-C226-4653-A84F-A4B5D40C11DE/anatomyfree.app/groups.xml, NSUnderlyingError=0x1702492a0 "Не удалось завершить операцию. 不允许操作" }

此文件存在。它已被解析,但不能从备份中排除。这是什么原因?

我使用 iOS 8.2、iPhone 6、ru 语言环境。

这里是方法调用:

NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSURL *url = [NSURL fileURLWithPath:path];
[Utilities addSkipBackupAttributeToItemAtURL:url];

【问题讨论】:

    标签: ios objective-c icloud


    【解决方案1】:

    试试这个,它在我的 iOS 8 中完美运行:

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.%@", documentsPath, fileName, extension]];
    [Utilities addSkipBackupAttributeToItemAtURL:url];
    

    【讨论】:

    • 没有帮助。原因:路径中不存在文件。
    • 我使用 .xml 文件进行解析。它存储在 Resources 文件夹中。
    【解决方案2】:

    在 NSURL *url = [NSURL fileURLWithPath:path] 之前添加这段代码

    if([[UIDevice currentDevice] systemVersion] floatValue] >= 8.0f){
        path = [NSString stringWithFormat:@"./%@",path];
    }
    

    【讨论】:

      【解决方案3】:

      我在使用时遇到了问题:

      pDirURL.setValue(true , forKey: NSURLIsExcludedFromBackupKey)
      

      效果很好。有一天它开始抛出NSUnknownKeyException,原因不明。现在我用

      try pDirURL.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)
      

      又可以正常使用了!

      【讨论】:

        【解决方案4】:

        它不工作的原因是因为你的资源文件是随你的应用程序(捆绑包)一起提供的,它不会也永远不会在用户的 iCloud 中备份。

        永远不会写入位于您的应用程序包中的资源,这就是任何写入/修改操作都会失败的原因。

        如果您真的想使用 iCloud 备份,您需要先将文件复制到像 NSSearchPathDirectory.DocumentDirectory 这样的目录中。

        【讨论】:

          猜你喜欢
          • 2016-04-07
          • 2015-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-08
          相关资源
          最近更新 更多