设置禁止Document和Library目录下的文件同步到iCloud

以下代码设置IOS Document和Library目录下的文件禁止同步到iCloud

//设置禁止云同步

-(void)addNotBackUpiCloud{

    

    NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);


    NSString *docPath = [docPaths objectAtIndex:0];

    NSString *libPath = [libPaths objectAtIndex:0];

    

    [self fileList:docPath];

    [self fileList:libPath];

    

}


- (void)fileList:(NSString*)directory{

    

    NSError *error = nil;

    NSFileManager * fileManager = [NSFileManager defaultManager];

    NSArray *fileList = [fileManager contentsOfDirectoryAtPath:directory error:&error];

    for (NSString* each in fileList) {

        NSMutableString* path = [[NSMutableString alloc]initWithString:directory];

        [path appendFormat:@"/%@",each];

        

        NSURL *filePath = [NSURL fileURLWithPath:path];

        

        [self addSkipBackupAttributeToItemAtURL:filePath];

        [self fileList:path];

    

    }

}



//设置禁止云同步

-(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL{

    

    double version = [[UIDevice currentDevice].systemVersion doubleValue];//判定系统版本。

    

    

    if(version >=5.1f){

        

        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;

        

    }

    const char* filePath = [[URL path] fileSystemRepresentation];

    

    const char* attrName = "com.apple.MobileBackup";

    u_int8_t attrValue = 1;

    

    int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);


    return result == 0;

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值