我的应用程序刚刚被拒绝,因为我在 NSDocumentDirectory 中复制了 sqlite 数据库并且没有将其排除在同步到 iCloud 之外。我尝试添加代码以将其从 iCloud 中排除,但它仍在 iCloud 中占用相同的空间。这是我的代码。
- (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
NSError *error = nil;
[URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
return error == nil;
}
}
+ (void)copyDatabaseIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSString *toPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:toPath];
if(!success) {
NSString *fromPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
NSURL *toURL = [NSURL fileURLWithPath:toPath];
NSURL *fromURL = [NSURL fileURLWithPath:fromPath];
[[NSFileManager defaultManager] addSkipBackupAttributeToItemAtURL:toURL];
[[NSFileManager defaultManager] addSkipBackupAttributeToItemAtURL:fromURL];
success = [fileManager copyItemAtURL:fromURL toURL:toURL error:&error];
if (!success)
NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
+ (NSString *)getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:databaseName];
}
请帮忙。