iOS - Flag entire Document directory as do not backup

前端 未结 2 1001
独厮守ぢ
独厮守ぢ 2020-12-29 09:04

My application got rejected by Apple due to :

2.23: Apps must follow the iOS Data Storage Guidelines or they will be rejected

so

相关标签:
2条回答
  • 2020-12-29 09:30

    You can mark as no-backup only the parent folder, not every file. But it should not be a Documents directory as it should contain only user-created content. You should use Library/Application Support (Technical Q&A QA1719)

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *directory = [paths objectAtIndex:0];
    directory = [directory stringByAppendingPathComponent:@"MyData"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:directory] == NO) {
    
        NSError *error;
        if ([fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&error] == NO) {
            NSLog(@"Error: Unable to create directory: %@", error);
        }
    
        NSURL *url = [NSURL fileURLWithPath:directory];
        // exclude downloads from iCloud backup
        if ([url setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error] == NO) {
            NSLog(@"Error: Unable to exclude directory from backup: %@", error);
        }
    }
    

    Credits: xinsight blog

    0 讨论(0)
  • 2020-12-29 09:39

    ... And finally my application has been approved . just follow this instruction :

    put this code on appDelegate.m (didFinishLunching)

    NSString *docsDir;
    NSArray *dirPaths;
    NSURL * finalURL;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    
    
    finalURL = [NSURL fileURLWithPath:docsDir];
    
    
    [self addSkipBackupAttributeToItemAtURL:finalURL];
    

    and skip backup :

    - (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;
    }
    
    0 讨论(0)
提交回复
热议问题