Still backups to iCloud even when addSkipBackupAtt

2019-02-23 18:23发布

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

2条回答
放荡不羁爱自由
2楼-- · 2019-02-23 18:49

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.

查看更多
神经病院院长
3楼-- · 2019-02-23 19:04

Did you try create new back up on your device from icloud settings?

查看更多
登录 后发表回答