I cannot seem to get the backup attribute parameter working from my code. No matter what I seem to do, the method returns false with “Unknown Error -1”. I have tried different iterations of the skip-backup method, but below is the latest I’m using, based on another post I found:
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
if (&NSURLIsExcludedFromBackupKey == nil) {
// iOS 5.0.1 and lower
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
} else {
// First try and remove the extended attribute if it is present
int result = getxattr(filePath, attrName, NULL, sizeof(u_int8_t), 0, 0);
if (result != -1) {
// The attribute exists, we need to remove it
int removeResult = removexattr(filePath, attrName, 0);
if (removeResult == 0) {
NSLog(@"Removed extended attribute on file %@", URL);
}
}
// Set the new key
return [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil];
}
}
My call to the method looks like the following (it always prints “not successful” during runtime):
if ([self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:filePath]]) {
NSLog(@"success!");
} else {
NSLog(@"not successful");
}
And the filePath is the NSString path to the documents folder’s image. I logged an example of an image path as follows:
/var/mobile/Applications/B3583D06-38BC-45F0-A027-E79997F0EC60/Documents/myImage.png
In my app, it’s looping through all the (numerous) files I push out there, and it’s adding this attribute. It fails on each and every one of them. I need to be able to get this working, due to a hard requirement of offline capabilities for these dynamically-driven files. I don’t see that many others are having this issue, so that tells me I may be missing something obvious here, lol.
I found this post: Why was my application still rejected after excluding files from iCloud backup using this code?
And that looks promising as to what my issue could be, but there are not really any details about how to specifically resolve (it just recommends to use /Library instead of the docs folder?).
Thanks to anyone in advance who can help with this!
-Vincent
In order to fix this, I ended up just having to create a folder within the /Documents directory, and then I changed the app to point all the files to that sub-folder.
So in my AppDelegate, before anything has loaded up-front, I added code like the following:
And when I did that, it actually got into the ‘did add attribute’ log statement with no issues! I have no idea why this didn’t work at the individual file level, but at least this works, and it’s actually a better solution, since now I didn’t have to loop through hundreds of files to add the attribute, and I only had to add it in one place.