美文网首页
ios 本地目录 通过URL 转 目录Path 方法

ios 本地目录 通过URL 转 目录Path 方法

作者: jasperWhf | 来源:发表于2019-12-27 17:14 被阅读0次

+(void) doDownload:(NSString*)localFilePath

{

 NSURLSessionConfiguration *configuration =[NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager =[[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSString *servicePath =[NSString stringWithFormat:DOWNLOAD_RESUME_FILE_URL, [enterpriseId stringByAppendingPathExtension:@"zip"]];

    NSURL *URL =[NSURL URLWithString:servicePath];

    NSURLRequest *request =[NSURLRequest requestWithURL:URL];

    // targetPath是下载的临时文件路径,:app_dir/tmp/CFNetworkDownload_9z499O.tmp

    NSURL*(^destinationBlock) (NSURL *targetPath, NSURLResponse *response) = ^NSURL* (NSURL *targetPath, NSURLResponse *response){

        return[NSURL fileURLWithPath:localFilePath];// 下载文件最终存放地址,不同于targetPath

    };

        // filePath即上面那个block的返回值

    void(^completionBlock) (NSURLResponse *response, NSURL *filePath, NSError *error) = ^void (NSURLResponse *response, NSURL *filePath, NSError *error){

               NSFileManager *fileManager =[NSFileManager defaultManager];

          NSString *zipFilePath =[filePath path];// NSURL转成NSString

                        if([fileManager fileExistsAtPath:zipFilePath]){

            [fileManager removeItemAtPath:zipFilePath error:nil];

        }    };

NSURLSessionDownloadTask *downloadTask =[manager downloadTaskWithRequest:request progress:nil destination:destinationBlock completionHandler:completionBlock];

    [downloadTask resume];

}

将NSString转成NSURL的API是fileURLWithPath,从NSURL转成NSString的API是path

相关文章

网友评论

      本文标题:ios 本地目录 通过URL 转 目录Path 方法

      本文链接:https://www.haomeiwen.com/subject/amvdoctx.html