iOS避免文件被同步到iCloud或iTunes

本文介绍了如何通过代码避免iOS应用中特定文件被同步至iCloud或iTunes,包括在沙盒创建文件夹、设置文件排除备份属性等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

App程序会同步程序到iCloud或iTunes,为了数据安全,可以通过代码避免指定文件被同步到苹果服务器


在沙盒创建一个文件


1
2
3
4
5
6
7
8
9
10
11
12
 - (void)createSkipBackupImagesFolder {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/images"];
 
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
        NSURL *toURL = [NSURL fileURLWithPath:dataPath];
        [self addSkipBackupAttributeToItemAtURL:toURL];
    }
}


避免该文件被同步到iCloud或iTunes,使用NSURLIsExcludedFromBackupKey    


1
2
3
4
5
6
7
8
9
10
11
12
13
- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
    {
    NSURL* URL= [NSURL fileURLWithPath: filePathString];
    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;
}

   

参考链接:https://developer.apple.com/library/ios/qa/qa1719/_index.html#//apple_ref/doc/uid/DTS40011342

http://stackoverflow.com/questions/12971192/how-should-i-prevent-files-from-being-backed-up-to-icloud-and-itunes-on-ios-5-0



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值