iOS - Flag entire Document directory as do not backup

前端 未结 2 1018
独厮守ぢ
独厮守ぢ 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条回答
  •  -上瘾入骨i
    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

提交回复
热议问题