2

我最近因为我的 NSLibraryDirectory 将错误类型的数据备份到 iCloud 而被拒绝。我试图阻止整个目录备份到 iCloud,因为这个目录只包含下载的内容。AppDelegate.m 中的这段代码可以工作吗?

- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

   NSURL *pathURL= [NSURL fileURLWithPath:documentsDirectory];
[self addSkipBackupAttributeToItemAtURL:pathURL];
return documentsDirectory;

}

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL

{ if (NSURLIsExcludedFromBackupKey) {
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);



NSError *error = nil;

BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]

                              forKey: NSURLIsExcludedFromBackupKey error: &error];

if(!success){

    NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);

}

return success;
}
else {
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);



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;
}

}
4

0 回答 0