Using addSkipBackupAttributeToItemAtURL to prevent backing up files - ios

I can't get this to work. My app was rejected for allowing files to be backed up that were unnecessary. To stop this I was told to use addSkipBackupAttributeToItemAtURL to mark the files not to be backed up.
I placed the following code, provided by Apple, in my AppDelegate.
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(#"Error excluding %# from backup %#", [URL lastPathComponent], error);
} else {
NSLog(#"\n\nFile %# removed from backup.\n\n", URL);
}
return success;
}
I then add the following in didFinishLaunchingWithOptions
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"blue" ofType:#"jpg"]]];
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"fred" ofType:#"jpg"]]];
It works fine in the simulator. On the actual phone though, it does't work and gives me what I think resolves to a permissions error.
Error excluding blue.jpg from backup Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x1740741c0 {NSURL=file:///private/var/mobile/Containers/Bundle/Application/C8559ABA-181E-4686-A5ED-2ADCA283D2E4/PetJournal.app/blue.jpg, NSFilePath=/private/var/mobile/Containers/Bundle/Application/C8559ABA-181E-4686-A5ED-2ADCA283D2E4/PetJournal.app/blue.jpg, NSUnderlyingError=0x174058e10 "The operation couldn’t be completed. Operation not permitted"}
Can anyone help guide me through this.

Related

Exclude from iCloud backup: NSURLIsExcludedFromBackupKey

My app had been rejected the 2nd times and I lost 3 weeks :(
The first submit, I excluded ONLY DIRECTORIES from being backed-up in iCloud. Apple rejected...
The second submit, I excluded DIRECTORIES & PICTURES downloaded from being backed-up in iCloud. Apple again rejected... Apple also complaint that I have no "Restore" feature for my In-App purchase, while in fact, I do have a "Restore" button and it worked when I tested it.
I've done as Apple had suggested by excluding the file from being backedup using NSURLIsExcludedFromBackupKey. There was an interesting comment made by Macmade's on stackoverflow here:
sometimes Apple reviewers think your data can be re-generated, when
it's not. Then you'll have to explain why the data has to be backed-up
How often do the reviewer misunderstood and we have to explain to them that the content is required offline & not re-generateable?
Here is the code I used to exclude my files & directories from iCloud. Do you spot any problems?
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
// There's a chance the download failed, but don't assert here
//assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue:[NSNumber numberWithBool:YES]
forKey:NSURLIsExcludedFromBackupKey
error: &error];
if(!success){
NSLog(#"Error excluding %# from backup %#", [URL lastPathComponent], error);
}
return success;
}
//Download picture from Google and exclude it from being backed-up in iCloud
- (void)downloadFetcher:(GTMHTTPFetcher *)fetcher
finishedWithData:(NSData *)data
error:(NSError *)error
{
if (error == nil) {
// successfully retrieved this photo's data; save it to disk
GDataEntryPhoto *photoEntry = [fetcher propertyForKey:#"photo entry"];
// Create album directory if it doesn't already exist
NSString *path = [self findOrCreateApplicationSupportSubPath:[photoEntry albumTitle]];
path = [path stringByAppendingPathComponent:[[photoEntry title] stringValue]];
if (path != nil) {
// Write to disk
BOOL didSave = [data writeToFile:path
options:NSDataWritingAtomic
error:&error];
if (didSave) {
// Exclude file from being backed up in iCloud
NSURL *url = [NSURL fileURLWithPath:path];
BOOL excludeBackupResult = [self addSkipBackupAttributeToItemAtURL:url];
if (excludeBackupResult == NO) {
NSLog(#"Error excluding FILE from iCloud: %#", path);
}
// Update the download progress bar
_downloadedFileCounter = _downloadedFileCounter + 1;
float progress = _downloadedFileCounter / kMaleWireframeImagesTotal;
[self updateProgress:progress];
// The download completed. -2 just incase a package is lost, but let the user move on...
if (_downloadedFileCounter >= _downloadableFilesTotal -2) {
[_panel6 downloadCompleted];
}
} else {
// error saving file. Perhaps out of space? Write permissions error?
NSLog(#"Save anatomy picture failed: %#", error.localizedDescription);
}
} else {
NSLog(#"downloadFetcher: Cannot create directory");
}
} else {
NSLog(#"downloadFetcher failed: %#", error);
}
}
//Create directory and exclude it from being backed-up in iCloud
-(NSString*)findOrCreateApplicationSupportSubPath:(NSString*)subPath
{
NSString *resolvedPath;
NSArray *appSupportDir = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
if ([appSupportDir count] != 0) {
resolvedPath = [appSupportDir objectAtIndex:0];
// Append the name of this application
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleExecutable"];
resolvedPath = [resolvedPath stringByAppendingPathComponent:executableName];
resolvedPath = [resolvedPath stringByAppendingPathComponent:subPath];
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:resolvedPath]) {
// Path doesn't exist, creates it
NSError *error;
BOOL successful = [manager createDirectoryAtPath:resolvedPath withIntermediateDirectories:YES attributes:nil error:&error];
if(!successful) {
NSLog(#"ERROR creating APP Support Sub-Directory: %#", error.localizedDescription);
return nil;
} else {
// Exclude path from backing-up in iCloud
NSURL *url = [NSURL fileURLWithPath:resolvedPath];
BOOL excludeBackupResult = [self addSkipBackupAttributeToItemAtURL:url];
if(!excludeBackupResult){
NSLog(#"Error excluding DIRECTORY from iCloud backup. This is a violation to their guideline.");
return nil;
}
}
}
} else {
NSLog(#"No Application Support Path available");
return nil;
}
return resolvedPath;
}
I think the trick is to add the NSURLIsExcludedFromBackupKey OR make sure the directory is outside the documents directory. I did this by moving my documents to the Library/Application Support folder (since it didn't make sense in the /tmp or /Caches folders):
- (void)createNewRefFolder
{
NSError *error;
// store in /Library/Application Support/BUNDLE_IDENTIFIER/Reference
// make sure Application Support folder exists
NSURL *applicationSupportDirectory = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:&error];
if (error) {
NSLog(#"KCDM: Could not create application support directory. %#", error);
}
NSURL *referenceFolder = [applicationSupportDirectory URLByAppendingPathComponent:#"Reference" isDirectory:YES];
if (![[NSFileManager defaultManager] createDirectoryAtPath:[referenceFolder path]
withIntermediateDirectories:YES
attributes:nil
error:&error]) {
NSLog(#"KCDM: Error creating Reference folder: %# ...", error);
}
BOOL success = [referenceFolder setResourceValue:#YES forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(#"KCDM: Error excluding %# from backup %#", referenceFolder, error);
}
}
Little UPDATE from previous answers.
You have to check the existence of the file. Otherwise, you will get this error,
Error excluding [FileName] from backup:
Error Domain=NSCocoaErrorDomain Code=4 "The file “[FileName]” doesn’t exist."
...
I am not sure if we should check for the value is already updated or not.
e.g. if the API reset already set value or not. If it tries to update the file system again for a set value that is more time consuming, I guess.
Updated method...
+ (BOOL)addSkipBackupAttributeToURLAtPath:(NSURL *)url
{
if (!url) return NO;
if (![[NSFileManager defaultManager] fileExistsAtPath:url.path]) return NO;
NSError *error = nil;
NSNumber *value = nil;
BOOL success = [url getResourceValue:&value forKey:NSURLIsExcludedFromBackupKey error:&error];
if (value.boolValue == YES) return YES;
success = [url setResourceValue:[NSNumber numberWithBool:YES]
forKey:NSURLIsExcludedFromBackupKey error:&error];
if(!success){
NSLog(#"Error excluding %# from backup: %#", [url lastPathComponent], error);
}
return success;
}
+ (BOOL)addSkipBackupAttributeToFileAtPath:(NSString *)path
{
if (!path) return NO;
return [self addSkipBackupAttributeToURLAtPath:[NSURL fileURLWithPath:path]];
}

iOS prevent files from being backed up to iCloud

in my app i have to store core data database. And i have some groups and folders with default data (audiofiles, maptiles, etc) in the xcode project navigator.
I found a lot about preventing files from being backed up like:
What i have done:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"db.sqlite"];
NSLog(#"%#", storeURL.path);
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
//NSDictionary *options = #{NSMigratePersistentStoresAutomaticallyOption:#YES, NSInferMappingModelAutomaticallyOption:#YES};
/*NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];*/
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
[self addSkipBackupAttributeToItemAtURL:storeURL];
return _persistentStoreCoordinator;
}
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
preventing method:
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(#"Error excluding %# from backup %#", [URL lastPathComponent], error);
}
return success;
}
(The minimum target iOS version is 7.0)
Is this enough? How can i check if the app now prevent backing up the core data database?
Before i added the addSkipBackupAttributeToItemAtURL method i checked the apps storage and found nothing under documents and data. I only found 3.1 MB under backups -> my iphone
- Install and launch your app
- Go to Settings > iCloud > Storage & Backup > Manage Storage
- If necessary, tap "Show all apps"
- Check your app's storage
You are doing it right.
Following the Apple Technical Q&A 1719: How do I prevent files from being backed up to iCloud and iTunes? you should mark with the "do not back up" attribute.
For NSURL objects, add the NSURLIsExcludedFromBackupKey attribute to prevent the corresponding file from being backed up. For CFURLRef objects, use the corresponding kCFURLIsExcludedFromBackupKey attribute.
To know which folder is more appropriated to store you Core Data database you can check the iOS Data Storage Guidelines.

Recommended place to save iOS game data without iCloud support

What is the recommended place to save game data if I do not want automatically to be backup to iCloud ? I am using NSUbiquitousKeyValueStore for iCloud, and have custom logic for updates.
So I do not for iCloud backup to mess things up.
By my understanding that is: Library/Caches/.
Because: Documents/ and Library/Preferences/ are backup to iCloud.
Am I correct ?
You shouldn't use /Library/Cache for storing such kind of information. Look what Apple Data Storage Guidelines says:
Data that can be downloaded again or regenerated should be stored in
the /Library/Caches directory.
You can save all your data to /Documents, just flag it so that it wouldn't copy to iCloud:
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(#"Error excluding %# from backup %#", [URL lastPathComponent], error);
}
return success;
}
You can use this method in your AppDelegate or wherever you create these files. Note that you can exlude from iCloud backup not only specific files, but directories too.
You can also exclude the specific game data from performing backup to iCloud.
NSURL *url = [NSURL fileURLWithPath:fileName];
assert([[NSFileManager defaultManager] fileExistsAtPath:fileName]);
NSError *error = nil;
BOOL success = [url setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(#"Error excluding %# from backup %#", [url lastPathComponent], error);
}
return success;

unable to delete my file in ios [duplicate]

This question already has answers here:
Cocoa - NSFileManager removeItemAtPath Not Working
(6 answers)
Closed 9 years ago.
i am trying to delete a file at a particular URL,
my code iS :
NSString *str= [outputFieldURL absoluteString];
NSError *error;
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:str error:&error];
if (!success) {
NSLog(#"Error removing file at path: %#", error.localizedDescription);
}
else
{
NSLog(#"File removed at path: %#", error.localizedDescription);
}
}
and am getting output :
Error removing file at path: The operation couldn’t be completed. (Cocoa error 4.)
where outputFieldURL is showing following value when i convert it into a string :
file:///var/mobile/Applications/A55A56FA-478D-4996-807D-12F0E968F969/Documents/301013125211w.m4a
this is the path where my audio with a format of .m4a is saved
Your path is incorrect
Use the following
NSString *str= [outputFieldURL path];
in place of
NSString *str= [outputFieldURL absoluteString];
The method "removeItemAtPath:" need the local path of file,
If you want to remove using url, you should use "removeItemAtURL:"
There may be case that file path which you provide is not correct. If its correct then try following with URL, it might solve your issue
NSString *str= [outputFieldURL absoluteString];
NSError *error;
NSURL *url = [NSURL URLWithString:str];
BOOL success = [[NSFileManager defaultManager] removeItemAtURL:url error:&error];
if (!success) {
NSLog(#"Error removing file at path: %#", error.localizedDescription);
}
else
{
NSLog(#"File removed at path: %#", error.localizedDescription);
}
}

"The operation couldn’t be completed. No such file or directory" when trying to move a file from temp

I'm trying to move a file from the tmp directory, but it's not working as expected. Here's the code i have, and the log:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(#"URL: %#", URL);
NSString *finalPath = [self fullPathForMediaDisplayType:JMediaDisplayTypeMovie];
NSLog(#"Final Path: %#", finalPath);
NSURL *finalURL = [NSURL fileURLWithPath:finalPath];
NSLog(#"Final URL: %#", finalURL);
NSError *error;
[fileManager removeItemAtURL:finalURL error:&error];
[fileManager moveItemAtURL:URL toURL:finalURL error:&error];
NSLog(#"Error: %#", error);
Log:
URL: file://localhost/private/var/mobile/Applications/APP-ID-REMOVED/tmp//trim.ptltHA.MOV
Final Path: /var/mobile/Applications/APP-ID-REMOVED/Documents/9i3bqQVsKZMXwjCr38My-movie.MOV
Final URL: file://localhost/var/mobile/Applications/APP-ID-REMOVED/Documents/9i3bqQVsKZMXwjCr38My-movie.MOV
Error: error: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x1f53fb30 {NSUnderlyingError=0x1f53fad0 "The operation couldn’t be completed. No such file or directory", NSFilePath=/var/mobile/Applications/APP-ID-REMOVED/Documents/9i3bqQVsKZMXwjCr38My-movie.mov, NSUserStringVariant=(
Remove
)}
Any ideas why this isn't recognising the directory? The documents directory definitely exists, I save all sorts of other files in there.

Resources