ios将NSURL转换成filepath
来源:互联网 发布:建立数据透视表的步骤 编辑:程序博客网 时间:2024/06/07 02:17
今天用AFNetworking做了下载文件的功能,但是API的返回类型是NSURL,在网上搜索了与NSString相互转换的代码,记录一下,删除了无关代码:
+(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
AFNetworking的核心API,需要传2个回调block作为参数,在我的注释里解释了含义,文档里也有。另外要注意,2个block都是跑在UI Thread里的
0 0
- ios将NSURL转换成filepath
- iOS NSURL 中文转换问题
- NSString转换成NSURL
- NSString filepath Convert to NSUrl filepath(文件路径 NSString和NSUrl转化)
- [ios] NSURL
- IOS NSURL
- NSString转换成NSURL为空
- NSString转换成NSURL为空
- iOS 解决NSString转换为NSURL时包含中文字符
- iOS中NSString转化成NSUrl
- ios网络编程网络接口里边含有汉字不能转换成NSURL并且调试显示为空
- iOS:”NSURL中使用string拼接有时候会使NSURL成nil“解决方案
- IOS NSURL基本操作
- iOS 网络 NSURL(1)
- iOS 网络 NSURL 2
- [IOS] 类 NSURL
- IOS开发之----NSURL
- IOS开发之NSURL
- 基于C#弹幕类射击游戏的实现——(二)渲染
- maven向本地仓库导入jar包命令
- 虚拟键盘按键的ASCII值
- Squid故障与解决方法汇总
- Mybatis启动报Result Maps collection already contains value for ...的问题
- ios将NSURL转换成filepath
- css样式加载顺序及覆盖顺序
- IOS 静态链接库
- 给2014年的中小手游开发商们,撑不下去就嫁了吧!
- 渐进时间复杂度即时间复杂度
- RTEMS 的libio ,work area ,heap,串口驱动等的一些阅读源代码分析笔记
- 基于轮廓线索的实时人体检测
- sqlserver递归查询
- JDBC连接各种数据库方法