- 浏览: 1029660 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
繁星水:
Useful!!
Object-C之(Null)与(Bool) -
pacer123:
请问注解@ApiModelProperty 在哪个jar包下面 ...
Micro Service工具集之Swagger:可测试的样式化API文档 -
sucheng2016:
这东东真的好用么/
对象转换利器之Dozer -
zzq0324:
http://git.oschina.net/zzq0324/ ...
基于Spring-WS的Restful API的集成测试 -
lrz0534:
你好,我在maven上面下载了swagger-springmv ...
Micro Service工具集之Swagger:可测试的样式化API文档
NSURLSession VS NSURLConnection
NSURLSession可以看做是NSURLConnection的进化版,其对NSURLConnection的改进点有:
知识点
用法
使用NSURLSession的一般套路如下:
实战
这儿我简单的实现了一个下载任务的断点续传功能,具体效果如下:
实现代码如下:
所有代码在这儿:https://github.com/xianlinbox/iOS7_New/tree/master/iOS7_New/NSURLSession/ViewController
参考文章:http://www.objc.io/issue-5/from-nsurlconnection-to-nsurlsession.html
http://www.shinobicontrols.com/blog/posts/2013/09/20/ios7-day-by-day-day-1-nsurlsession
NSURLSession可以看做是NSURLConnection的进化版,其对NSURLConnection的改进点有:
- * 根据每个Session做配置(http header,Cache,Cookie,protocal,Credential),不再在整个App层面共享配置.
- * 支持网络操作的取消和断点续传
- * 改进了授权机制的处理
- * 丰富的Delegate模型
- * 分离了真实数据和网络配置数据。
- * 后台处理上传和下载,即使你点击了“Home”按钮,后台仍然可以继续下载,并且提供了根据网络状况,电力情况进行处理的配置。
知识点
用法
使用NSURLSession的一般套路如下:
- 1. 定义一个NSURLRequest
- 2. 定义一个NSURLSessionConfiguration,配置各种网络参数
- 3. 使用NSURLSession的工厂方法获取一个所需类型的NSURLSession
- 4. 使用定义好的NSURLRequest和NSURLSession构建一个NSURLSessionTask
- 5. 使用Delegate或者CompletionHandler处理任务执行过程的所有事件。
实战
这儿我简单的实现了一个下载任务的断点续传功能,具体效果如下:
实现代码如下:
#import "UrlSessionDemoViewController.h" @interface UrlSessionDemoViewController () @end @implementation UrlSessionDemoViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; return self; } - (void)viewDidLoad { [super viewDidLoad]; self.progressBar.progress = 0; } - (NSURLSession *)session { //创建NSURLSession NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil]; return session; } - (NSURLRequest *)request { //创建请求 NSURL *url = [NSURL URLWithString:@"http://p1.pichost.me/i/40/1639665.png"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; return request; } -(IBAction)start:(id)sender { //用NSURLSession和NSURLRequest创建网络任务 self.task = [[self session] downloadTaskWithRequest:[self request]]; [self.task resume]; } -(IBAction)pause:(id)sender { NSLog(@"Pause download task"); if (self.task) { //取消下载任务,把已下载数据存起来 [self.task cancelByProducingResumeData:^(NSData *resumeData) { self.partialData = resumeData; self.task = nil; }]; } } -(IBAction)resume:(id)sender { NSLog(@"resume download task"); if (!self.task) { //判断是否又已下载数据,有的话就断点续传,没有就完全重新下载 if (self.partialData) { self.task = [[self session] downloadTaskWithResumeData:self.partialData]; }else{ self.task = [[self session] downloadTaskWithRequest:[self request]]; } } [self.task resume]; } //创建文件本地保存目录 -(NSURL *)createDirectoryForDownloadItemFromURL:(NSURL *)location { NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *urls = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]; NSURL *documentsDirectory = urls[0]; return [documentsDirectory URLByAppendingPathComponent:[location lastPathComponent]]; } //把文件拷贝到指定路径 -(BOOL) copyTempFileAtURL:(NSURL *)location toDestination:(NSURL *)destination { NSError *error; NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtURL:destination error:NULL]; [fileManager copyItemAtURL:location toURL:destination error:&error]; if (error == nil) { return true; }else{ NSLog(@"%@",error); return false; } } #pragma mark NSURLSessionDownloadDelegate - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { //下载成功后,文件是保存在一个临时目录的,需要开发者自己考到放置该文件的目录 NSLog(@"Download success for URL: %@",location.description); NSURL *destination = [self createDirectoryForDownloadItemFromURL:location]; BOOL success = [self copyTempFileAtURL:location toDestination:destination]; if(success){ // 文件保存成功后,使用GCD调用主线程把图片文件显示在UIImageView中 dispatch_async(dispatch_get_main_queue(), ^{ UIImage *image = [UIImage imageWithContentsOfFile:[destination path]]; self.imageView.image = image; self.imageView.contentMode = UIViewContentModeScaleAspectFit; self.imageView.hidden = NO; }); }else{ NSLog(@"Meet error when copy file"); } self.task = nil; } /* Sent periodically to notify the delegate of download progress. */ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { //刷新进度条的delegate方法,同样的,获取数据,调用主线程刷新UI double currentProgress = totalBytesWritten/(double)totalBytesExpectedToWrite; dispatch_async(dispatch_get_main_queue(), ^{ self.progressBar.progress = currentProgress; self.progressBar.hidden = NO; }); } - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes { } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
所有代码在这儿:https://github.com/xianlinbox/iOS7_New/tree/master/iOS7_New/NSURLSession/ViewController
参考文章:http://www.objc.io/issue-5/from-nsurlconnection-to-nsurlsession.html
http://www.shinobicontrols.com/blog/posts/2013/09/20/ios7-day-by-day-day-1-nsurlsession
发表评论
-
Xcode的代码结构管理
2015-07-21 22:34 2706Merge Conflicts on .project fil ... -
记Protocol Oriented Programming in Swift of WWDC 2015
2015-07-12 20:28 1784其实最先朋友让我就这个题目写篇文章的时候,我是拒绝的,因为觉得 ... -
你应该知道的Core Animation知识
2015-06-07 00:04 1261分享最近读的一本书:https://github.com/At ... -
iOS7之定制View Controller切换效果
2014-04-18 23:19 38334在iOS5和iOS6前,View Controller的切换主 ... -
Object-C之(Null)与(Bool)
2014-04-05 14:08 9056在写iOS程序的时候,想初始化一个空值的时候,经常会迷茫到底是 ... -
iOS之Local Notification(本地通知)·
2014-03-23 21:31 19449Local Notification的作用 Local Not ... -
实战iOS7之后台多任务
2014-02-08 20:20 15209在WWDC2013中 水果公司把iOS7的中后台多任务单独开出 ... -
iOS概念之KVO(Key-Value Observing)
2014-01-26 20:21 21720在一个复杂的,有状态的系统中,当一个对象的状态发生改变,如何通 ... -
实战iOS7之UIDynamics
2014-01-26 14:26 10439最近在看Sam Davies写的iOS7系列文章(http:/ ... -
在iOS App的图标上显示版本信息
2013-12-29 19:46 5563最近读到一篇文章(http://www.merowing.in ... -
构建iOS持续集成平台
2013-09-25 20:33 5330之前写的关于iOS持续集成平台的文章终于在infoQ上发表了, ... -
Object-C的对象模型
2013-09-17 20:04 2388在Object-C的类型结构中,有几个比较重要的概念Objec ... -
使用AVAudioRecorder,AVAudioPlayer构建简单录音笔应用
2013-07-12 05:57 16859在iOS的基础类库中, 提供了AVFoundation Fra ... -
UIView的Animation效果
2013-07-05 19:05 23688所谓动画效果,就是会动的画,到iOS App中来说的话,就是各 ... -
iOS使用GameKit构建“Master-Client”网络
2013-07-02 20:34 2086GameKit是iOS SDK中一个常用的框架。其核心功能有3 ... -
Object-C中的块(Block)
2013-06-26 20:22 9157在Object-C中,块的概念 ... -
Object-C动态特性之protocol
2013-06-25 16:11 8549在Object-C中,Protocol被用来定义一组独立于具体 ... -
Object-C中的动态特性Category
2013-06-22 08:41 1672刚接触Object-C哪会儿, ... -
统一iOS客户端和服务器端认证
2013-01-18 21:05 3342最近公司的同事业余时间搞了一个内部的类about. ... -
iOS开发随笔记之 常量声明和NSUserDefaults
2012-12-09 14:19 5029在iOS中,常量声明一般使用2种方式 1、使用C语言的预处理方 ...
相关推荐
在iOS中,NSURLSession支持在app未运行或挂起时进行后台下载。此外,NSURLSession原生的支持data、file、ftp、http和https URL方案,以及用户首选项中代理和socks网关。 这篇文章通过下载、暂停、恢复下载图片来...
NSURLSession三种Task的详细Demo。希望对想要学习IOS网络开发的童鞋会有帮助。欢迎关注我的IOS-SDK详解专栏,如果你是个初学者,这里你会找到很多资源。...
支持后台下载,ios7以上系统,支持视频播放
主要介绍了IOS网络请求之NSURLSession使用详解,今天使用NSURLConnection分别实现了get、post、表单提交、文件上传、文件下载,有兴趣的可以了解一下。
基于NSURLSession NSURLProtol的UIWebView离线缓存 主要是使用苹果的黑魔法类:NSURLProtocol来对网络请求进行拦截,拦截后使用自定义的网络去加载数据后进行离线缓存。这样保证在没有网络的情况下,也能保证离线能...
ios使用Afnetworking和Nsurlsession测试,包含多种用法
iOS 7 编程实战 英文版 iOS 7 编程实战 英文版 iOS 7 编程实战 英文版 iOS 7 编程实战 英文版 iOS 7 编程实战 英文版 iOS 7 编程实战 英文版 iOS 7 编程实战 英文版 中文版也有
基于NSURLSession NSURLProtol的UIWebView离线缓存 主要是使用苹果的黑魔法类:NSURLProtocol来对网络请求进行拦截,拦截后使用自定义的网络去加载数据后进行离线缓存。这样保证在没有网络的情况下,也能保证离线能...
swift nsurlsession实现并发网络请求
使用NSURLSession进行的下载上传以及断点续传,可多个文件上传demo
这个教程将会教会你如何在后台传输数据,如何使用iOS7提供的多任务API。我将会教会你如何在后台下载一个文件,并且在文件下载完成时弹出一个本地的提醒。 后台数据传输起源于iOS6,允许在前台或者后台下载数据,但是...
Swift编程实战 iOS应用开发实例及完整解决方案 pdf 清晰版本
最近项目中刚好用到这个请求,就写下来了,方便日后使用,请求没什么展示性的就不传图了
文件下载(NSURLSession)
这是之前封装的下载工具类,支持开始,暂停,继续,删除,断点下载,后台下载,进度记录,app杀死后可以继续下载。GIT地址:https://github.com/famile/Download。如果觉得不错记得star
iOS 7 编程实战 带书签目录 高清完整版
斯坦福ios7公开课纸牌源码(stanford ios7)
使用技术: 基于iOS最新的网络请NSURLSession 封装下载框架 目前功能: (1)多线程下载,可自定义并发数 (2)支持断点续传 (3)支持取消下载任务 (4)支持暂停下载后app重新启动可继续下载 (5)支持状态栏网络...