您可以使用 NSFileNanager 列出所有文件,然后调用您喜欢的建议的函数。您的代码将类似于:
// From Apple FAQ
#import <sys/xattr.h>
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
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;
}
- (void) addSkipBackupAttributeToItemsInFolder:(NSString*)folder
{
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:folder error:nil];
for (int curFileIdx = 0; curFileIdx < [dirContents count]; ++curFileIdx)
{
NSString* curString = [folder stringByAppendingPathComponent:[dirContents objectAtIndex:curFileIdx]];
NSURL* curFileUrl = [NSURL fileURLWithPath:curString];
[self addSkipBackupAttributeToItemAtURL: curFileUrl];
}
}
你会这样使用它:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[self addSkipBackupAttributeToItemsInFolder:documentsDirectory];