文件夹的预防iCloud云备份(Prevent iCloud Backup of Folder)

2019-09-17 17:21发布

最近我拒绝了我的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;
}

}
文章来源: Prevent iCloud Backup of Folder