在iPhone Documents文件夹中引用iCloud +媒体存储,该文件夹显示如何为文件设置iOS 5.0.1"不备份"属性.
是否有一种有效的方法可以为整个文件夹/文件层次结构执行此操作?例如,我的应用程序创建/ Library/PrivateDocs,并使用多个文件夹,子文件夹和文件填充它.我可以在顶级文件夹中设置do-not-backup属性,还是必须在其下的每个文件和文件夹上设置?
并且,如果必须在每个文件/子文件夹上设置,那么这样做的有效方法是什么?
k1t*_*1th 25
为此目的,您可以在文档目录中放置一个特定目录,将所有内容放入其中并仅将该单个目录标记为do-not-backup使用
addSkipBackupAttributeToItemAtURL
Run Code Online (Sandbox Code Playgroud)
您链接的文章中显示的方法,或使用此方法使用路径而不是URL:
+ (void)addSkipBackupAttributeToPath:(NSString*)path {
u_int8_t b = 1;
setxattr([path fileSystemRepresentation], "com.apple.MobileBackup", &b, 1, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)
使用目录的示例:
NSString *docsDirPath = [(AppDelegate*)[[UIApplication sharedApplication] delegate] applicationDocumentsDirectory];
NSString *yourContentsDirPath = [docsDirPath stringByAppendingPathComponent:gContentsDirName];
[Utilities addSkipBackupAttributeToPath:yourContentsDirPath];
Run Code Online (Sandbox Code Playgroud)
[编辑]
我忘了在委托中使用这个方法来获取文件目录:
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
Run Code Online (Sandbox Code Playgroud)
对于我在这里的所有朋友,谁需要在Swift中 - 只需看下面(iOS8.0)
func addSkipBackupAttributeToItemAtURL(URL:NSURL) ->Bool{
let fileManager = NSFileManager.defaultManager()
assert(fileManager.fileExistsAtPath(URL.absoluteString))
var error:NSError?
let success:Bool = URL.setResourceValue(NSNumber.numberWithBool(true),forKey: NSURLIsExcludedFromBackupKey, error: &error)
if !success{
println("Error excluding \(URL.lastPathComponent) from backup \(error)")
}
return success;
}
Run Code Online (Sandbox Code Playgroud)