在iOS 5.0或更低版本上,setxattr以errno 2(ENOENT)失败

时间:2012-07-20 14:14:11

标签: ios5 backup icloud

我将一些持久性数据存储在/ Documents目录中,该应用程序从API调用中获取并且需要启动应用程序。 Apple拒绝了建议跳过iCloud备份的应用程序。

我使用了https://developer.apple.com/library/ios/#qa/qa1719/_index.html

中描述的代码

if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1
        assert([[NSFileManager defaultManager] fileExistsAtPath: path]);

        const char* filePath = [path fileSystemRepresentation];
        printf("[path fileSystemRepresentation] %s\n",filePath);
        const char* attrName = "com.apple.MobileBackup";
        u_int8_t attrValue = 1;
        int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
        NSLog(@"errno %d", errno);
        return result == 0;
    } else { // iOS >= 5.1
        NSURL* url = [NSURL fileURLWithPath:path];
        NSError *error = nil;
        BOOL success = [url setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
        NSLog(@"Error excluding %@ from backup %@", [url lastPathComponent], error);
        return success;

    }

适用于iOS 5.1模拟器。我使用

检查了文件系统
$xattr "filename" 

它告诉我

filename: com.apple.metadata:com_apple_backup_excludeItem

然而,它不适用于5.0模拟器以及我测试过的5.0.1 3GS设备。

我逐步通过代码并在if部分(&lt; = 5.0.1)中,我还按照Apple的技术问答文章中的建议打印了全局var errno。但是对setxattr的调用失败,并且errno打印为2,根据http://developer.apple.com/library/ios/#documentation/system/conceptual/manpages_iphoneos/man2/intro.2.html ENOENT

我也打印出filePath并且它不是空的并正确打印出来。我使用iExplorer检查过,文件在设备上以及Simulator文件夹中,它们都被创建了。

有人遇到过这个吗?不确定如何解决这个问题?

任何帮助都非常感谢!

谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

好的......我的坏......它正确地做了一切。它应该返回0表示成功执行setxattr ...但它仍然返回一个错误代码,它会使我绊倒...无论如何代码正在按预期工作。