我標誌着我的圖片文件夾不進行備份,因爲我下載圖像供脫機使用。 Here蘋果討論如何做到這一點。
#include<sys/xattr.h>
- (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)createSkipBackupImagesFolder {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/images"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSURL *toURL = [NSURL fileURLWithPath:dataPath];
[self addSkipBackupAttributeToItemAtURL:toURL];
}
}
[self createSkipBackupImagesFolder];
我以前在這裏提供了這個問題的確切解決方案:http://stackoverflow.com/q/12371321/1633251 - 如果你喜歡問與答請upvote兩者。 –