Error excluding ApplicationSupportDirectory from Backup

断了今生、忘了曾经 提交于 2020-01-25 09:52:07

问题


MY App got rejected because it backsUp too much data to iCloud.

In order to solve the problem, I have to put all of my files in a new directory, located in ApplicationSupportDirectory. I am not managing to do it so far and I cannot figure out what seems to be the problem.

This is my code so far:

AppDelegate.m class:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{

NSString* string = [[self applicationDocumentsDirectory] absoluteString];

if (![[NSFileManager defaultManager] fileExistsAtPath:string  isDirectory:NULL]) {
    NSError *error = nil;
    [[NSFileManager defaultManager] createDirectoryAtPath:string 
withIntermediateDirectories:YES attributes:nil error:&error];
    if (error){
        NSLog(@"I'm not even making your stupid dir");
    }
}
[self addSkipBackupAttributeToItemAtURL:[self applicationDocumentsDirectory]];
return YES;
}

- (NSURL *)applicationDocumentsDirectory
{

NSString *appSupportDir =   
[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask,  
YES) lastObject];
appSupportDir = [appSupportDir stringByAppendingPathComponent:@"MyAppDirectory"];
NSLog(appSupportDir);
return [NSURL URLWithString:appSupportDir];}


- (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;
}

But at the end, I just get an error: [1535:60b] Error excluding (null) from backup (null);

Is there anything Im missing? For a start, what's bugging me is - is there any line of code I should write to even tell that I want all my side in that specific folder, that I later want to skip backing up? if I should do that, where should I? AppDelegate.m or somewhere else?

This part of AppDevelopment is something I never came across before, so I'm really lost in here.


回答1:


It seems like you don't create your directory anywhere. Before calling addSkipBackupAttributeToItemAtURL add a verification if the directory is there and create it if needed.

    if (![[NSFileManager defaultManager] fileExistsAtPath:[self applicationDocumentsDirectory] isDirectory:NULL]) {
            NSError *error = nil;        
            [[NSFileManager defaultManager] createDirectoryAtPath:[self applicationDocumentsDirectory] withIntermediateDirectories:YES attributes:nil error:&error];
            if (error){
                 //Something went wrong
            }
    }


来源:https://stackoverflow.com/questions/23408210/error-excluding-applicationsupportdirectory-from-backup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!