我的应用程序被拒绝,因为似乎 7 MB 存储在文档文件夹中,并且它们会自动发送到 icloud。所以我循环了所有将通过这种方法写入文档文件夹的文件:
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL {
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
if (&NSURLIsExcludedFromBackupKey == nil) {
// iOS 5.0.1 and lower
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
}
else {
// First try and remove the extended attribute if it is present
int result = getxattr(filePath, attrName, NULL, sizeof(u_int8_t), 0, 0);
if (result != -1) {
// The attribute exists, we need to remove it
int removeResult = removexattr(filePath, attrName, 0);
if (removeResult == 0) {
NSLog(@"Removed extended attribute on file %@", URL);
}
}
// Set the new key
NSError *error = nil;
[URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
return error == nil;
}
此代码实现后,我的应用程序的 1.1 版获得批准。上周我尝试发送同一个应用程序的 1.2 版(文件管理没有任何变化,存储在文档文件夹中的所有文件都通过 addSkipBackupAttributeToItemAtURL 方法循环)。由于同样的原因,我的应用程序再次被拒绝。我无法将我的文件移动到临时文件夹或缓存文件夹,因为我的应用程序无法完全恢复文件(其中一个文件是数据库,恢复数据库意味着丢失任何用户插入的数据),所以这不是解决方案.无论如何,我在代码中发现了一个问题,这就是我调用该方法的方式:
[自己添加SkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:fullPath]];
在 ios 5.1 中使用 [NSURL fileURLWithPath:fullPath] 设备会返回错误,而且似乎无法创建该属性。如果我使用 [NSURL URLWithString:defaultStorePath] 更改 nsurl 的初始化,5.1 的设备似乎可以正确添加该属性。
在 ios 5.0.1 中,一切都被反转了,[NSURL URLWithString:defaultStorePath] 在 [NSURL fileURLWithPath:fullPath] 工作时返回错误。
也许我可以检查 ios 版本并设置适当的 nsurl 初始化,但它仍然是一个问题。在拒绝解释中,我读到:
特别是,我们发现在启动和/或内容下载时,您的应用存储 7mb。要检查您的应用存储了多少数据:
- 安装并启动您的应用
- 转到“设置”>“iCloud”>“存储和备份”>“管理存储”
- 如有必要,点按“显示所有应用”
- 检查您应用的存储空间
如果我尝试检查这个值,我会看到 7 MB 也有正确的 nsurl 初始化(当所有属性都正确设置时)。什么是正确的行为?有人有这个问题吗?在苹果建议的应用程序存储检查之前,我是否必须做一些特定的事情才能使其变得重要?
最佳答案
我认为诀窍是添加 NSURLIsExcludedFromBackupKey 并确保该目录位于文档目录之外。我通过将我的文档移动到 Library/Application Support 文件夹来做到这一点(因为它在/tmp 或/Caches 文件夹中没有意义):
// store in /Library/Application Support/BUNDLE_IDENTIFIER/Reference
// make sure Application Support folder exists
NSURL *applicationSupportDirectory = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:&error];
if (error) {
NSLog(@"KCDM: Could not create application support directory. %@", error);
return nil;
}
NSURL *referenceFolder = [applicationSupportDirectory URLByAppendingPathComponent:@"Reference" isDirectory:YES];
if (![[NSFileManager defaultManager] createDirectoryAtPath:[referenceFolder path]
withIntermediateDirectories:YES
attributes:nil
error:&error]) {
NSLog(@"KCDM: Error creating Reference folder to store model %@: %@", modelName, error);
return nil;
}
BOOL success = [referenceFolder setResourceValue:@YES forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(@"KCDM: Error excluding %@ from backup %@", referenceFolder, error);
}
关于ios - NSURLIsExcludedFromBackupKey - 应用程序必须遵循 iOS 数据存储指南,否则将被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11204903/