Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Still backups to iCloud even when addSkipBackupAttributeToItemAtURL is implemented?

My recent iOS app just got rejected because the application is storing data on Documents so it is backed up by iCloud, this is not allowed since the data is downloaded from a server.

I'm trying to rebuild the code right now and have hit a tough spot when storing the data in the a folder calles Application Support instead.

But even though I'm using addSkipBackupAttributeToItemAtURL it still shows up as a backup to iCloud.

I'm only targeting 5.1 so it's not a versioning problem.

I've added the affected code here below:

    NSString *newPath = [NSMutableString stringWithFormat:@"%@/Library/Application Support/", NSHomeDirectory()];

    if (![[NSFileManager defaultManager] fileExistsAtPath:newPath]) //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:newPath
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }

    }

    NSString *guidesPath = [newPath stringByAppendingPathComponent:@"/Guides"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:guidesPath])  //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:guidesPath
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }

    }

    NSString *dataPath = [guidesPath stringByAppendingPathComponent:dbName];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }

    }

    NSString *newGuidesPath = [dataPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *guidesURL = [NSURL URLWithString:newGuidesPath];
    [self addSkipBackupAttributeToItemAtURL:guidesURL];

    NSString* path = [dataPath stringByAppendingPathComponent: 
                      [NSString stringWithString: finalName] ];
    if (![fileManager fileExistsAtPath:path]) {
        [myData writeToFile:path atomically:YES];
        NSString *docFile = [dataPath stringByAppendingPathComponent: @"guideid.txt"];
        [[guideID dataUsingEncoding:NSUTF8StringEncoding] writeToFile:docFile atomically:NO];
        DLog(@"Saved database here:%@", path);
    }else{
        DLog(@"Already exists, no need to copy");
    }

}

 - (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;
}
like image 485
Joakim Engstrom Avatar asked Jan 17 '23 13:01

Joakim Engstrom


1 Answers

Actually found a solution, I've encoded the URL which was not needed. Just added this to the folder

    NSURL *guidesURL = [NSURL fileURLWithPath:guidesPath];
    [guidesURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:NULL];

And skipped the exclude method, just flagged it inside the code, which seems to work.

like image 118
Joakim Engstrom Avatar answered Jan 19 '23 03:01

Joakim Engstrom