iOS iCloud 백업 제외하기!

개발/iOS 2017. 10. 19. 09:48
반응형

iOS 등록하는데 앱 승인 거부를 당했습니다.

리젝(reject) 사유를 보니 Document 에 리소스들이 있는데 이것들은 뭐냐는 거였습니다.


그래서 자료를 찾아보니 Document 폴더내 파일들이 iCloud로 자동백업되고 있었습니다.유저들은 필요없는 파일인데 말이죠.

iCloud 사용공간이 낭비되는것을 막고, 승인거절을 해결하기 위해 백업에 제외하는 방법을 알아보도록 하겠습니다.


iCloud 백업을 막기 위해서는 NSURL 객체의 'setResourceValue:forKey:error:' 메소드로 해결 할 수 있습니다. 속성값으로 'forKey'에 "NSURLIsExcludedFromBackupKey"를 셋팅하면 됩니다. 


사용코드를 살펴 보겠습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-(void)setResourceNoBackup
{
    @try {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        NSArray *documents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:basePath error:nil];
        NSURL *URL;
        NSString *completeFilePath;
        NSError *error = nil;
        for (NSString *file in documents) {
            
            completeFilePath = [NSString stringWithFormat:@"%@/%@", basePath, file];
            
            URL = [NSURL fileURLWithPath:completeFilePath];
            //[URL resourceValuesForKeys:[NSArray arrayWithObject:NSURLIsExcludedFromBackupKey] error:nil];
            
            BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                          forKey: NSURLIsExcludedFromBackupKey error:&error];
            
            if (success)
            {
                NSLog(@"File Success : %@", file );
            }
            else
            {
                NSLog(@"File Fail : %@", file );
            }
            // NSLog(@"File %@  is excluded from backup %@", file, [URL resourceValuesForKeys:[NSArray arrayWithObject:NSURLIsExcludedFromBackupKey] error:nil]);
        }
    } @catch (NSException *exception) {
        ;
    } @finally {
        ;
    }
    
}
cs


위 코드는 Document의 모든 폴더 및 파일을 iCloud 백업을 제외하고 있습니다. 필요에 의한 필터링이 필요하시면 직접 구현하셔도 됩니다.


참고로 for문의 NSString *file 에 폴더명이 들어가면 하위 폴더까지 백업에서 제외 됩니다. 그러니 구지 하위 폴더에 접근해서 일일이 'setResourceValue:forKey:error:'를 호출 할 필요는 없습니다. 






반응형
admin