AFNetworking 2.0 新特性讲解之AFHTTPSessionManager

AFNetworking 2.0 相比1.0 API 接口改动还是很大的. 其中一个便是 AFURLSessionManager,当然如果你不太熟悉,或者为了兼容低版本,你依然可以选择AFHTTPRequestOperationManager,AFURLSessionManager是基于

NSURLSessionConfiguration(IOS 7.0+, MAC OX10_9+)的.

下面我们借用TuneStore的API 来完成AFURLSessionManager的讲解.

如果您在看这边文章之前 我建议您首先阅读下AFNetworking2.0的官方新特性介绍.

  1. AFNetworking 2.0 Migration Guide - 你如果之前用过AF1.0 这个是很有必要来看的.

  2. AFNetworking 2.0 Docs

  3. AFNetworking 2.0 Source


另外T*witer 上说NSHipster的AFNetworking现在翻译成中文了,所以我搬过来了有兴趣的朋友也可以去看下,https://github.com/NSHipster/articles/blob/zh-Hans/2013-09-16-afnetworking-2.md


1:创建 client search iTunes


<span class="com" style="color: rgb(102, 116, 123);">#import "AFHTTPSessionManager.h"</span><span class="pln" style="color: rgb(241, 242, 243);">
</span><span class="lit" style="color: rgb(250, 205, 34);">@interface</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="typ" style="color: rgb(103, 140, 177);">ITunesClient</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="typ" style="color: rgb(103, 140, 177);">AFHTTPSessionManager</span><span class="pln" style="color: rgb(241, 242, 243);">
</span><span class="pun" style="color: rgb(241, 242, 243);">+</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="typ" style="color: rgb(103, 140, 177);">ITunesClient</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*)</span><span class="pln" style="color: rgb(241, 242, 243);">sharedClient</span><span class="pun" style="color: rgb(241, 242, 243);">;</span><span class="pln" style="color: rgb(241, 242, 243);">
</span><span class="pun" style="color: rgb(241, 242, 243);">-</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="typ" style="color: rgb(103, 140, 177);">NSURLSessionDataTask</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*)</span><span class="pln" style="color: rgb(241, 242, 243);">searchForTerm</span><span class="pun" style="color: rgb(241, 242, 243);">:(</span><span class="typ" style="color: rgb(103, 140, 177);">NSString</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*)</span><span class="pln" style="color: rgb(241, 242, 243);">term completion</span><span class="pun" style="color: rgb(241, 242, 243);">:(</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="kwd" style="color: rgb(147, 199, 99);">void</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">(^)(</span><span class="typ" style="color: rgb(103, 140, 177);">NSArray</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">results</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="typ" style="color: rgb(103, 140, 177);">NSError</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">error</span><span class="pun" style="color: rgb(241, 242, 243);">)</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">)</span><span class="pln" style="color: rgb(241, 242, 243);">completion</span><span class="pun" style="color: rgb(241, 242, 243);">;</span><span class="pln" style="color: rgb(241, 242, 243);">
</span><span class="lit" style="color: rgb(250, 205, 34);">@end</span>

<span class="com" style="color: rgb(102, 116, 123);">#import "ITunesClient.h"</span><span class="pln" style="color: rgb(241, 242, 243);">

</span><span class="lit" style="color: rgb(250, 205, 34);">@implementation</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="typ" style="color: rgb(103, 140, 177);">ITunesClient</span><span class="pln" style="color: rgb(241, 242, 243);">


</span><span class="pun" style="color: rgb(241, 242, 243);">+</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="typ" style="color: rgb(103, 140, 177);">ITunesClient</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*)</span><span class="pln" style="color: rgb(241, 242, 243);">sharedClient </span><span class="pun" style="color: rgb(241, 242, 243);">{</span><span class="pln" style="color: rgb(241, 242, 243);">
    </span><span class="kwd" style="color: rgb(147, 199, 99);">static</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="typ" style="color: rgb(103, 140, 177);">ITunesClient</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">_sharedClient </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="kwd" style="color: rgb(147, 199, 99);">nil</span><span class="pun" style="color: rgb(241, 242, 243);">;</span><span class="pln" style="color: rgb(241, 242, 243);">
    </span><span class="kwd" style="color: rgb(147, 199, 99);">static</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="typ" style="color: rgb(103, 140, 177);">dispatch_once_t</span><span class="pln" style="color: rgb(241, 242, 243);"> onceToken</span><span class="pun" style="color: rgb(241, 242, 243);">;</span><span class="pln" style="color: rgb(241, 242, 243);">
    dispatch_once</span><span class="pun" style="color: rgb(241, 242, 243);">(&</span><span class="pln" style="color: rgb(241, 242, 243);">onceToken</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">^{</span><span class="pln" style="color: rgb(241, 242, 243);">
        NSURL </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">baseURL </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="pln" style="color: rgb(241, 242, 243);">NSURL </span><span class="typ" style="color: rgb(103, 140, 177);">URLWithString</span><span class="pun" style="color: rgb(241, 242, 243);">:@</span><span class="str" style="color: rgb(236, 118, 0);">"https://itunes.apple.com/"</span><span class="pun" style="color: rgb(241, 242, 243);">];</span><span class="pln" style="color: rgb(241, 242, 243);">

        </span><span class="typ" style="color: rgb(103, 140, 177);">NSURLSessionConfiguration</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">config </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="typ" style="color: rgb(103, 140, 177);">NSURLSessionConfiguration</span><span class="pln" style="color: rgb(241, 242, 243);"> defaultSessionConfiguration</span><span class="pun" style="color: rgb(241, 242, 243);">];</span><span class="pln" style="color: rgb(241, 242, 243);">
        </span><span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="pln" style="color: rgb(241, 242, 243);">config setHTTPAdditionalHeaders</span><span class="pun" style="color: rgb(241, 242, 243);">:@{</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">@</span><span class="str" style="color: rgb(236, 118, 0);">"User-Agent"</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">@</span><span class="str" style="color: rgb(236, 118, 0);">"TuneStore iOS 1.0"</span><span class="pun" style="color: rgb(241, 242, 243);">}];</span><span class="pln"><span style="color: rgb(241, 242, 243);">
        </span></span><p class="p1" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: rgb(51, 204, 0);"><span class="s1">        </span><span class="s2">//</span>设置我们的缓存大小<span class="s2"> </span>其中内存缓存大小设置<span class="s2">10M  </span>磁盘缓存<span class="s2">5M</span></span></p><span style="color: rgb(241, 242, 243);">        </span><span class="typ" style="color: rgb(103, 140, 177);">NSURLCache</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">cache </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">[[</span><span class="typ" style="color: rgb(103, 140, 177);">NSURLCache</span><span class="pln" style="color: rgb(241, 242, 243);"> alloc</span><span class="pun" style="color: rgb(241, 242, 243);">]</span><span class="pln" style="color: rgb(241, 242, 243);"> initWithMemoryCapacity</span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="lit" style="color: rgb(250, 205, 34);">10</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="lit" style="color: rgb(250, 205, 34);">1024</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="lit" style="color: rgb(250, 205, 34);">1024</span><span class="pln" style="color: rgb(241, 242, 243);">
                                                          diskCapacity</span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="lit" style="color: rgb(250, 205, 34);">50</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="lit" style="color: rgb(250, 205, 34);">1024</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="lit" style="color: rgb(250, 205, 34);">1024</span><span class="pln" style="color: rgb(241, 242, 243);">
                                                              diskPath</span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="kwd" style="color: rgb(147, 199, 99);">nil</span><span class="pun" style="color: rgb(241, 242, 243);">];</span><span class="pln" style="color: rgb(241, 242, 243);">
        
        </span><span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="pln" style="color: rgb(241, 242, 243);">config setURLCache</span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);">cache</span><span class="pun" style="color: rgb(241, 242, 243);">];</span><span class="pln" style="color: rgb(241, 242, 243);">
        
        _sharedClient </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">[[</span><span class="typ" style="color: rgb(103, 140, 177);">ITunesClient</span><span class="pln" style="color: rgb(241, 242, 243);"> alloc</span><span class="pun" style="color: rgb(241, 242, 243);">]</span><span class="pln" style="color: rgb(241, 242, 243);"> initWithBaseURL</span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);">baseURL
                                         sessionConfiguration</span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);">config</span><span class="pun" style="color: rgb(241, 242, 243);">];</span><span class="pln" style="color: rgb(241, 242, 243);">
        _sharedClient</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">responseSerializer </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="typ" style="color: rgb(103, 140, 177);">AFJSONResponseSerializer</span><span class="pln" style="color: rgb(241, 242, 243);"> serializer</span><span class="pun" style="color: rgb(241, 242, 243);">];</span><span class="pln" style="color: rgb(241, 242, 243);">
    </span><span class="pun" style="color: rgb(241, 242, 243);">});</span><span class="pln" style="color: rgb(241, 242, 243);">
    
    </span><span class="kwd" style="color: rgb(147, 199, 99);">return</span><span class="pln" style="color: rgb(241, 242, 243);"> _sharedClient</span><span class="pun" style="color: rgb(241, 242, 243);">;</span><span class="pln" style="color: rgb(241, 242, 243);">
</span><span class="pun" style="color: rgb(241, 242, 243);">}</span><span class="pln" style="color: rgb(241, 242, 243);">

</span><span class="pun" style="color: rgb(241, 242, 243);">-</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="typ" style="color: rgb(103, 140, 177);">NSURLSessionDataTask</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*)</span><span class="pln" style="color: rgb(241, 242, 243);">searchForTerm</span><span class="pun" style="color: rgb(241, 242, 243);">:(</span><span class="typ" style="color: rgb(103, 140, 177);">NSString</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*)</span><span class="pln" style="color: rgb(241, 242, 243);">term completion</span><span class="pun" style="color: rgb(241, 242, 243);">:(</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="kwd" style="color: rgb(147, 199, 99);">void</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">(^)(</span><span class="typ" style="color: rgb(103, 140, 177);">NSArray</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">results</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="typ" style="color: rgb(103, 140, 177);">NSError</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">error</span><span class="pun" style="color: rgb(241, 242, 243);">)</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">)</span><span class="pln" style="color: rgb(241, 242, 243);">completion </span><span class="pun" style="color: rgb(241, 242, 243);">{</span><span class="pln" style="color: rgb(241, 242, 243);">
    </span><span class="typ" style="color: rgb(103, 140, 177);">NSURLSessionDataTask</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">task </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="kwd" style="color: rgb(147, 199, 99);">self</span><span class="pln" style="color: rgb(241, 242, 243);"> GET</span><span class="pun" style="color: rgb(241, 242, 243);">:@</span><span class="str"><span style="color: rgb(236, 118, 0);">"/search"</span></span><p class="p1" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: rgb(51, 255, 51);">                                  <span class="s1">//</span><span class="s2">为了速度我们将地区设置为台湾</span></span></p><span class="pln" style="color: rgb(241, 242, 243);">                                parameters</span><span class="pun" style="color: rgb(241, 242, 243);">:@{</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">@</span><span class="str" style="color: rgb(236, 118, 0);">"country"</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">@</span><span class="str" style="color: rgb(236, 118, 0);">"TW"</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);">
                                              </span><span class="pun" style="color: rgb(241, 242, 243);">@</span><span class="str" style="color: rgb(236, 118, 0);">"term"</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);"> term </span><span class="pun" style="color: rgb(241, 242, 243);">}</span><span class="pln" style="color: rgb(241, 242, 243);">
                                   success</span><span class="pun" style="color: rgb(241, 242, 243);">:^(</span><span class="typ" style="color: rgb(103, 140, 177);">NSURLSessionDataTask</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">task</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> id responseObject</span><span class="pun" style="color: rgb(241, 242, 243);">)</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">{</span><span class="pln" style="color: rgb(241, 242, 243);">
                                       </span><span class="typ" style="color: rgb(103, 140, 177);">NSHTTPURLResponse</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">httpResponse </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="typ" style="color: rgb(103, 140, 177);">NSHTTPURLResponse</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*)</span><span class="pln" style="color: rgb(241, 242, 243);">task</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">response</span><span class="pun" style="color: rgb(241, 242, 243);">;</span><span class="pln" style="color: rgb(241, 242, 243);">
                                       </span><span class="kwd" style="color: rgb(147, 199, 99);">if</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="pln" style="color: rgb(241, 242, 243);">httpResponse</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">statusCode </span><span class="pun" style="color: rgb(241, 242, 243);">==</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="lit" style="color: rgb(250, 205, 34);">200</span><span class="pun" style="color: rgb(241, 242, 243);">)</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">{</span><span class="pln" style="color: rgb(241, 242, 243);">
                                           dispatch_async</span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="pln" style="color: rgb(241, 242, 243);">dispatch_get_main_queue</span><span class="pun" style="color: rgb(241, 242, 243);">(),</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">^{</span><span class="pln" style="color: rgb(241, 242, 243);">
                                               completion</span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="pln" style="color: rgb(241, 242, 243);">responseObject</span><span class="pun" style="color: rgb(241, 242, 243);">[@</span><span class="str" style="color: rgb(236, 118, 0);">"results"</span><span class="pun" style="color: rgb(241, 242, 243);">],</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="kwd" style="color: rgb(147, 199, 99);">nil</span><span class="pun" style="color: rgb(241, 242, 243);">);</span><span class="pln" style="color: rgb(241, 242, 243);">
                                           </span><span class="pun" style="color: rgb(241, 242, 243);">});</span><span class="pln" style="color: rgb(241, 242, 243);">
                                       </span><span class="pun" style="color: rgb(241, 242, 243);">}</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="kwd" style="color: rgb(147, 199, 99);">else</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">{</span><span class="pln" style="color: rgb(241, 242, 243);">
                                           dispatch_async</span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="pln" style="color: rgb(241, 242, 243);">dispatch_get_main_queue</span><span class="pun" style="color: rgb(241, 242, 243);">(),</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">^{</span><span class="pln" style="color: rgb(241, 242, 243);">
                                               completion</span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="kwd" style="color: rgb(147, 199, 99);">nil</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="kwd" style="color: rgb(147, 199, 99);">nil</span><span class="pun" style="color: rgb(241, 242, 243);">);</span><span class="pln" style="color: rgb(241, 242, 243);">
                                           </span><span class="pun" style="color: rgb(241, 242, 243);">});</span><span class="pln" style="color: rgb(241, 242, 243);">
                                           </span><span class="typ" style="color: rgb(103, 140, 177);">NSLog</span><span class="pun" style="color: rgb(241, 242, 243);">(@</span><span class="str" style="color: rgb(236, 118, 0);">"Received: %@"</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> responseObject</span><span class="pun" style="color: rgb(241, 242, 243);">);</span><span class="pln" style="color: rgb(241, 242, 243);">
                                           </span><span class="typ" style="color: rgb(103, 140, 177);">NSLog</span><span class="pun" style="color: rgb(241, 242, 243);">(@</span><span class="str" style="color: rgb(236, 118, 0);">"Received HTTP %d"</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> httpResponse</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">statusCode</span><span class="pun" style="color: rgb(241, 242, 243);">);</span><span class="pln" style="color: rgb(241, 242, 243);">
                                       </span><span class="pun" style="color: rgb(241, 242, 243);">}</span><span class="pln" style="color: rgb(241, 242, 243);">
                                       
                                   </span><span class="pun" style="color: rgb(241, 242, 243);">}</span><span class="pln" style="color: rgb(241, 242, 243);"> failure</span><span class="pun" style="color: rgb(241, 242, 243);">:^(</span><span class="typ" style="color: rgb(103, 140, 177);">NSURLSessionDataTask</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">task</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="typ" style="color: rgb(103, 140, 177);">NSError</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">error</span><span class="pun" style="color: rgb(241, 242, 243);">)</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">{</span><span class="pln" style="color: rgb(241, 242, 243);">
                                       dispatch_async</span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="pln" style="color: rgb(241, 242, 243);">dispatch_get_main_queue</span><span class="pun" style="color: rgb(241, 242, 243);">(),</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">^{</span><span class="pln" style="color: rgb(241, 242, 243);">
                                           completion</span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="kwd" style="color: rgb(147, 199, 99);">nil</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> error</span><span class="pun" style="color: rgb(241, 242, 243);">);</span><span class="pln" style="color: rgb(241, 242, 243);">
                                       </span><span class="pun" style="color: rgb(241, 242, 243);">});</span><span class="pln" style="color: rgb(241, 242, 243);">
                                   </span><span class="pun" style="color: rgb(241, 242, 243);">}];</span><span class="pln" style="color: rgb(241, 242, 243);">
    </span><span class="kwd" style="color: rgb(147, 199, 99);">return</span><span class="pln" style="color: rgb(241, 242, 243);"> task</span><span class="pun" style="color: rgb(241, 242, 243);">;</span><span class="pln" style="color: rgb(241, 242, 243);">
</span><span class="pun" style="color: rgb(241, 242, 243);">}</span><span class="pln" style="color: rgb(241, 242, 243);">
</span><span class="lit" style="color: rgb(250, 205, 34);">@end</span>
Note:为了便于对这个任务进行(取消,监控等)操作 我们将这个任务返回.


2: 在view controller对client进行回调


<span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="typ" style="color: rgb(103, 140, 177);">NSURLSessionDataTask</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">task </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">[[</span><span class="typ" style="color: rgb(103, 140, 177);">ITunesClient</span><span class="pln" style="color: rgb(241, 242, 243);"> sharedClient</span><span class="pun" style="color: rgb(241, 242, 243);">]</span><span class="pln" style="color: rgb(241, 242, 243);"> searchForTerm</span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);">term
                                                                 completion</span><span class="pun" style="color: rgb(241, 242, 243);">:^(</span><span class="typ" style="color: rgb(103, 140, 177);">NSArray</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">results</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="typ" style="color: rgb(103, 140, 177);">NSError</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">error</span><span class="pun" style="color: rgb(241, 242, 243);">)</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">{</span><span class="pln" style="color: rgb(241, 242, 243);">
                                                                     </span><span class="kwd" style="color: rgb(147, 199, 99);">if</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="pln" style="color: rgb(241, 242, 243);">results</span><span class="pun" style="color: rgb(241, 242, 243);">)</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">{</span><span class="pln" style="color: rgb(241, 242, 243);">
                                                                         </span><span class="kwd" style="color: rgb(147, 199, 99);">self</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">results </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> results</span><span class="pun" style="color: rgb(241, 242, 243);">;</span><span class="pln" style="color: rgb(241, 242, 243);">
                                                                         </span><span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="kwd" style="color: rgb(147, 199, 99);">self</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">tableView reloadData</span><span class="pun" style="color: rgb(241, 242, 243);">];</span><span class="pln" style="color: rgb(241, 242, 243);">
                                                                     </span><span class="pun" style="color: rgb(241, 242, 243);">}</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="kwd" style="color: rgb(147, 199, 99);">else</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">{</span><span class="pln" style="color: rgb(241, 242, 243);">
                                                                         </span><span class="typ" style="color: rgb(103, 140, 177);">NSLog</span><span class="pun" style="color: rgb(241, 242, 243);">(@</span><span class="str" style="color: rgb(236, 118, 0);">"ERROR: %@"</span><span class="pun" style="color: rgb(241, 242, 243);">,</span><span class="pln" style="color: rgb(241, 242, 243);"> error</span><span class="pun" style="color: rgb(241, 242, 243);">);</span><span class="pln" style="color: rgb(241, 242, 243);">
                                                                     </span><span class="pun" style="color: rgb(241, 242, 243);">}</span><span class="pln" style="color: rgb(241, 242, 243);">
                                                                 </span><span class="pun" style="color: rgb(241, 242, 243);">}];</span>

3:  AFNetworking UIKit 之 加载网络图片

每一个cell 我们可以利用 AFNetworking对UIImageView的 category .只需要在文件中导入 #import "UIImageView+AFNetworking.h" ,然后 cellForRowAtIndexPath:方法下直接使用即可

<span class="pln" style="color: rgb(241, 242, 243);">    cell</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">artworkImageView</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">image </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="kwd" style="color: rgb(147, 199, 99);">nil</span><span class="pun" style="color: rgb(241, 242, 243);">;</span><span class="pln" style="color: rgb(241, 242, 243);">
    </span><span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="pln" style="color: rgb(241, 242, 243);">cell</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">artworkImageView cancelImageRequestOperation</span><span class="pun" style="color: rgb(241, 242, 243);">];</span><span class="pln" style="color: rgb(241, 242, 243);">

    NSURL </span><span class="pun" style="color: rgb(241, 242, 243);">*</span><span class="pln" style="color: rgb(241, 242, 243);">imageURL </span><span class="pun" style="color: rgb(241, 242, 243);">=</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="pln" style="color: rgb(241, 242, 243);">NSURL </span><span class="typ" style="color: rgb(103, 140, 177);">URLWithString</span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);">record</span><span class="pun" style="color: rgb(241, 242, 243);">[@</span><span class="str" style="color: rgb(236, 118, 0);">"artworkUrl100"</span><span class="pun" style="color: rgb(241, 242, 243);">]];</span><span class="pln" style="color: rgb(241, 242, 243);">
    </span><span class="kwd" style="color: rgb(147, 199, 99);">if</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">(</span><span class="pln" style="color: rgb(241, 242, 243);">imageURL</span><span class="pun" style="color: rgb(241, 242, 243);">)</span><span class="pln" style="color: rgb(241, 242, 243);"> </span><span class="pun" style="color: rgb(241, 242, 243);">{</span><span class="pln" style="color: rgb(241, 242, 243);">
        </span><span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="pln" style="color: rgb(241, 242, 243);">cell</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">artworkImageView setImageWithURL</span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);">imageURL</span><span class="pun" style="color: rgb(241, 242, 243);">];</span><span class="pln" style="color: rgb(241, 242, 243);">
    </span><span class="pun" style="color: rgb(241, 242, 243);">}</span>

4:当任务运行时显示 activity indicator 

导入 #import "UIActivityIndicatorView+AFNetworking.h"  

<span class="pun" style="color: rgb(241, 242, 243);">[</span><span class="kwd" style="color: rgb(147, 199, 99);">self</span><span class="pun" style="color: rgb(241, 242, 243);">.</span><span class="pln" style="color: rgb(241, 242, 243);">activityIndicator setAnimatingWithStateOfTask</span><span class="pun" style="color: rgb(241, 242, 243);">:</span><span class="pln" style="color: rgb(241, 242, 243);">task</span><span class="pun" style="color: rgb(241, 242, 243);">];</span>

运行效果如下 




demo 下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值