使用apple建议代码不排除icloud备份中的文件

时间:2014-11-26 06:55:11

标签: objective-c icloud

由于icloud备份问题,我有一个被拒绝3次的应用。 Apple已经回信说我需要使用一些代码来排除备份文件。然而,这不起作用,我在智慧结束。

这是我用过的代码

- (BOOL)downloadFile:(NSString *)fileURI targetFolder:(NSString *)targetFolder targetFilename:(NSString *) targetFilename{
    @try{

        NSError *error = nil;
        NSURL *url = [NSURL URLWithString:fileURI];
        if(![url setResourceValue:@"YES" forKey:NSURLIsExcludedFromBackupKey error:&error]){
            NSLog(@"KCDM: Error excluding %@ from backup %@", fileURI, error);
        }else{
            NSData *urlData = [NSData dataWithContentsOfURL:url];

            if ( urlData )
            {
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];
                NSString *dataPath = [documentsDirectory stringByAppendingString:targetFolder];
                NSError *error = nil;

                if(![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
                    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error];
                }

                NSString *filePath = [NSString stringWithFormat:@"%@/%@%@", documentsDirectory,targetFolder,targetFilename];


                return [urlData writeToFile:filePath atomically:YES];
            }
        }
    }
    @catch(NSException * e){
        NSLog(@"Error download: %@",e);
    }

    return false;
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您尝试为从网上下载的NSURLIsExcludedFromBackupKey - Url设置http://。那不会奏效。

您必须为设备上保存的实际文件设置此键值对。 此外,您不应将此值设置为字符串@"YES",您必须使用表示布尔值YES的NSNumber对象。

例如:

NSString *filePath = [NSString stringWithFormat:@"%@/%@%@", documentsDirectory,targetFolder,targetFilename];
if ([urlData writeToFile:filePath atomically:YES]) {
    // did write correctly
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    if(![fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:&error]){
        NSLog(@"KCDM: Error excluding %@ from backup %@", fileURI, error);
        return NO;
    }
    // could set NSURLIsExcludedFromBackupKey 
    return YES;
}
// could not write to file
return NO;