The way apple storage guidelines is creating more problem for me because most of the data i am maintaining from the Documents directory (files,dataBase and some kind of app related stuff).Recently i uploaded a binary file to the app store it was rejected and apple provided me a report according to this point i am going to change my code as a below
- (NSString *)applicationDocumentsDirectory {
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *pathURL= [NSURL fileURLWithPath:documentPath];
[self addSkipBackupAttributeToItemAtURL:pathURL];
return documentPath;
}
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
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;
}
MY QUESTIONS:
1.can i use addSkipBackupAttributeToItemAtURL: method directly to the documents directory to privent the iCloud backup for my all files in the documents directory.
2.Above mentioned code is enough for my app to be approve in app store in case my last binary rejected due to "do not back up" attribute not included for my documents directory.
You should be able to set this attribute to a folder in order to avoid backup of the complete folder.
Note however that it might noch be a good approach to do this to the complete Documents folder. First of all this will create a situation where you're app has no backed-up content thus on a restore of the phone the App will be in vanilla state. I could also imagine that this just is not the way "as Apple intended" and thus could lead to rejection of the app (always a guesswork). If possible, I would create a subfolder for your non-backed-up content in the Document directory and put everything there (this might need some migration code though if you already have a version of this app on the store).
Note that the storage guidelines do allow storage of user-created / non-recreatable content in the Documents directory and you only have to mark things like downloaded content etc. that you cannot put in the Caches directory (for example if the user expects this content to be available offline).
Use this function
-(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
NSError *error = nil;
[URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
return error == nil;
}
}
this is implemeted code.thanks