iOS URL Loading System

(一) 概述

URL Loading System 提供了访问URL指向的server端内容的能力,核心就是NSURL类。该系统提供:下载、上传、管理cookie、缓存、认证以及编写定制的协议扩展。

该系统支持以下协议:ftp://http://https://file:///data://

(另外,iOS提供了用safari打开网页的方法,参见UIApplicationopenURL:


(二)框架图


1. URL Loading 概览

1)iOS 7 以及以后的版本,最好用NSURLSession来做URL request。

2)如果为了支持旧版本,可以用NSURLConnection来做,下载URL内容到内存(如需要,可写入硬盘)。


2. 获取内容到内存(in-memory)

1)对于简单的情况,用 NSURLSessionNSURL 就够了。

2)对于复杂情况,比如上传数据,用 NSURLSessionNSURLRequest 或者 NSURLConnectionNSURLRequest 就够了。

无论是那种方式,获取response都有两种方法:a)设置一个callback的block;b)设置一个delegate。


3. 获取内容到硬盘(on-disk)

1)对于简单的情况,用 NSURLSession +NSURL 就够了。

2)对于复杂情况,用 NSURLSession +NSURLRequest 或者 NSURLDownload +NSURLRequest 就够了。

注意:NSURLSession NSURLDownload 有优势:NSURLSession可以一直下载,即使你的app被挂起了、kill掉了、或者崩溃了也不影响下载。


(三)编程API

有两种方式:NSURLSessionNSURLConnection,但NSURLConnection用起来相对简单一些。

1. 用 NSURLConnection

a)同步方式:sendSynchronousRequest:returningResponse:error:

b)异步block方式:sendAsynchronousRequest:queue:completionHandler:

c)异步delegate方式:NSURLConnectionDelegateNSURLConnectionDownloadDelegateNSURLConnectionDataDelegate


2. 用NSURLSession

NSURLSessionNSURLConnection爽的地方,就在于当app没有运行(挂起、终止、崩溃)的时候,依然可以在后台下载、上传。这种方式实际上是把下载任务让另外一个独立的进程干了。当下载完成后,系统就在后台启动app,并传给相应的参数以接收数据。这一部分不详细说了,可以参考资料:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html#//apple_ref/doc/uid/TP40013509-SW1


(四)常用代码

1. 用 NSURLConnection 访问 URL

NSString *urlAsString = @"http://www.cnn.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];


[NSURLConnection
         sendAsynchronousRequest:urlRequest
         queue:[[NSOperationQueue alloc] init] // [NSOperationQueue mainQueue]
         completionHandler:^(NSURLResponse *response,
                             NSData *data,
                             NSError *error) 
        {
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
         if ([data length] >0 && error == nil && [httpResponse statusCode] == 200)
         {

                        // DO YOUR WORK HERE
                        // the returned data is usually json format
                        // use NSJSONSerialization api to parse the data

         }
         else if ([data length] == 0 && error == nil)
         {
             NSLog(@"Nothing was downloaded.");
         }
         else if (error != nil){
             NSLog(@"Error = %@", error);
         }

     }];

2. 用  NSURLSession  访问 URL

例子:假设我们要访问伦敦的天气,先定义一个url:

NSString *londonWeatherUrl =
  @"http://api.openweathermap.org/data/2.5/weather?q=London,uk";

我们先用 NSURLConnection 的方式实现:

NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:londonWeatherUrl]];
 
[NSURLConnection sendAsynchronousRequest:request
   queue:[NSOperationQueue mainQueue]
   completionHandler:^(NSURLResponse *response,
                       NSData *data,
                       NSError *connectionError) {
      // handle response
}];

再用 NSURLSession实现:

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
          completionHandler:^(NSData *data,
                              NSURLResponse *response,
                              NSError *error) {
            // handle response
 
  }] resume];

注意,我们是用了最简单、最快速的方法使用 NSURLSession。我们甚至没有指定queue,它会自动在后台执行下载任务。

另外,也可以使用AFNetworking来实现:

NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:londonWeatherUrl]];
 
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
    success:^(NSURLRequest *request,
              NSHTTPURLResponse *response,
              id JSON) {
    // handle response
} failure:nil];
[operation start];

AFNetworking的好处是返回的数据直接是json格式的,省的自己再解析了。


参考文章:

1. apple官方:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165-BCICJDHA

2. 著名的 Tutorial 系列:

http://www.raywenderlich.com/51127/nsurlsession-tutorial

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值