继ASIHTTPRequest发布不再维护的消息之后,如果我们不使用CDN(云服务器),AFNetworking会是一套不错的选择 。
下載網址:https://github.com/AFNetworking/AFNetworking
下载之后,直接进入Xcode的工程即可以用,记得加入SystemConfiguration.framework
范例参考:
在application: didFinishLaunchingWithOptions: 加入AFNetworkActivityIndicatorManager
记得
#import "AFNetworkActivityIndicatorManager.h"
<span style="color: rgb(2, 32, 0);">-</span> <span style="color: rgb(2, 32, 0);">(</span><span style="color: rgb(166, 19, 144);">BOOL</span><span style="color: rgb(2, 32, 0);">)</span>application<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(2, 32, 0);">(</span>UIApplication <span style="color: rgb(2, 32, 0);">*</span><span style="color: rgb(2, 32, 0);">)</span>application didFinishLaunchingWithOptions<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(2, 32, 0);">(</span><span style="color: rgb(64, 8, 0);">NSDictionary</span> <span style="color: rgb(2, 32, 0);">*</span><span style="color: rgb(2, 32, 0);">)</span>launchOptions <span style="color: rgb(2, 32, 0);">{</span> self.window <span style="color: rgb(2, 32, 0);">=</span> <span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(2, 32, 0);">[</span>UIWindow alloc<span style="color: rgb(2, 32, 0);">]</span> initWithFrame<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(2, 32, 0);">[</span>UIScreen mainScreen<span style="color: rgb(2, 32, 0);">]</span> bounds<span style="color: rgb(2, 32, 0);">]</span><span style="color: rgb(2, 32, 0);">]</span> autorelease<span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(2, 32, 0);">[</span>AFNetworkActivityIndicatorManager sharedManager<span style="color: rgb(2, 32, 0);">]</span> setEnabled<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(166, 19, 144);">YES</span><span style="color: rgb(2, 32, 0);">]</span>; self.window.backgroundColor <span style="color: rgb(2, 32, 0);">=</span> <span style="color: rgb(2, 32, 0);">[</span>UIColor whiteColor<span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(2, 32, 0);">[</span>self.window makeKeyAndVisible<span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(166, 19, 144);">return</span> <span style="color: rgb(166, 19, 144);">YES</span>; <span style="color: rgb(2, 32, 0);">}</span>
这是我们常用的下载网路资源(JSON 格式)
记得
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"
#import "JSONKit.h"
<span style="color: rgb(64, 8, 0);">NSURL</span> <span style="color: rgb(2, 32, 0);">*</span>url <span style="color: rgb(2, 32, 0);">=</span> <span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(64, 8, 0);">NSURL</span> URLWithString<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(191, 29, 26);">@</span><span style="color: rgb(191, 29, 26);">"http://www.domain.com"</span><span style="color: rgb(2, 32, 0);">]</span>; AFHTTPClient <span style="color: rgb(2, 32, 0);">*</span>httpClient <span style="color: rgb(2, 32, 0);">=</span> <span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(2, 32, 0);">[</span>AFHTTPClient alloc<span style="color: rgb(2, 32, 0);">]</span> initWithBaseURL<span style="color: rgb(2, 32, 0);">:</span>url<span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(64, 8, 0);">NSString</span> <span style="color: rgb(2, 32, 0);">*</span>_path<span style="color: rgb(2, 32, 0);">=</span><span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(64, 8, 0);">NSString</span> stringWithFormat<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(191, 29, 26);">@</span><span style="color: rgb(191, 29, 26);">"/user_login/%@/%@/"</span>,userName,password<span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(64, 8, 0);">NSMutableURLRequest</span> <span style="color: rgb(2, 32, 0);">*</span>request <span style="color: rgb(2, 32, 0);">=</span> <span style="color: rgb(2, 32, 0);">[</span>httpClient requestWithMethod<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(191, 29, 26);">@</span><span style="color: rgb(191, 29, 26);">"POST"</span> path<span style="color: rgb(2, 32, 0);">:</span>_path parameters<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(166, 19, 144);">nil</span><span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(2, 32, 0);">[</span>httpClient release<span style="color: rgb(2, 32, 0);">]</span>; AFHTTPRequestOperation <span style="color: rgb(2, 32, 0);">*</span>operation <span style="color: rgb(2, 32, 0);">=</span> <span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(2, 32, 0);">[</span>AFHTTPRequestOperation alloc<span style="color: rgb(2, 32, 0);">]</span> initWithRequest<span style="color: rgb(2, 32, 0);">:</span>request<span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(2, 32, 0);">[</span>operation setCompletionBlockWithSuccess<span style="color: rgb(2, 32, 0);">:^</span><span style="color: rgb(2, 32, 0);">(</span>AFHTTPRequestOperation <span style="color: rgb(2, 32, 0);">*</span>operation, <span style="color: rgb(166, 19, 144);">id</span> responseObject<span style="color: rgb(2, 32, 0);">)</span> <span style="color: rgb(2, 32, 0);">{</span> <span style="color: rgb(17, 116, 160);"><em>//下载成功之后,使用JSONKit将字串转成NSDictionary或NSArray 格式</em></span> <span style="color: rgb(64, 8, 0);">NSDictionary</span> <span style="color: rgb(2, 32, 0);">*</span>deserializedData <span style="color: rgb(2, 32, 0);">=</span> <span style="color: rgb(2, 32, 0);">[</span>operation.responseString objectFromJSONString<span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(2, 32, 0);">}</span> failure<span style="color: rgb(2, 32, 0);">:^</span><span style="color: rgb(2, 32, 0);">(</span>AFHTTPRequestOperation <span style="color: rgb(2, 32, 0);">*</span>operation, <span style="color: rgb(64, 8, 0);">NSError</span> <span style="color: rgb(2, 32, 0);">*</span>error<span style="color: rgb(2, 32, 0);">)</span> <span style="color: rgb(2, 32, 0);">{</span> <span style="color: rgb(17, 116, 160);"><em>//下载失败的处理</em></span> <span style="color: rgb(2, 32, 0);">}</span><span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(64, 8, 0);">NSOperationQueue</span> <span style="color: rgb(2, 32, 0);">*</span>queue <span style="color: rgb(2, 32, 0);">=</span> <span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(64, 8, 0);">NSOperationQueue</span> alloc<span style="color: rgb(2, 32, 0);">]</span> init<span style="color: rgb(2, 32, 0);">]</span> autorelease<span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(2, 32, 0);">[</span>queue addOperation<span style="color: rgb(2, 32, 0);">:</span>operation<span style="color: rgb(2, 32, 0);">]</span>;
我们做完上一个步骤,有時候会得到一些图片的绝对地址,接下來就是根据这些URL地址,进行异步下载图片。
记得 #import "UIImageView+AFNetworking.h"
简单的做法是
<span style="color: rgb(2, 32, 0);"> [</span>imageView setImageWithURL<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(191, 29, 26);">@</span><span style="color: rgb(191, 29, 26);">"图片的绝对路径"</span><span style="color: rgb(2, 32, 0);">]</span>;
复杂的做法,可以在图片下载完成之后,再去触发一些事件
<span style="color: rgb(64, 8, 0);">NSURL</span> <span style="color: rgb(2, 32, 0);">*</span>url <span style="color: rgb(2, 32, 0);">=</span> <span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(64, 8, 0);">NSURL</span> URLWithString<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(191, 29, 26);">@</span><span style="color: rgb(191, 29, 26);">图片的绝对路径</span>"<span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(64, 8, 0);">NSMutableURLRequest</span> <span style="color: rgb(2, 32, 0);">*</span>request <span style="color: rgb(2, 32, 0);">=</span> <span style="color: rgb(2, 32, 0);">[</span><span style="color: rgb(64, 8, 0);">NSMutableURLRequest</span> requestWithURL<span style="color: rgb(2, 32, 0);">:</span>url cachePolicy<span style="color: rgb(2, 32, 0);">:</span>NSURLRequestUseProtocolCachePolicy timeoutInterval<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(36, 13, 144);">30.0</span><span style="color: rgb(2, 32, 0);">]</span>; <span style="color: rgb(2, 32, 0);">[</span>imageView setImageWithURLRequest<span style="color: rgb(2, 32, 0);">:</span>request placeholderImage<span style="color: rgb(2, 32, 0);">:</span><span style="color: rgb(166, 19, 144);">nil</span> success<span style="color: rgb(2, 32, 0);">:^</span><span style="color: rgb(2, 32, 0);">(</span><span style="color: rgb(64, 8, 0);">NSURLRequest</span> <span style="color: rgb(2, 32, 0);">*</span>request, <span style="color: rgb(64, 8, 0);">NSHTTPURLResponse</span> <span style="color: rgb(2, 32, 0);">*</span>response, UIImage <span style="color: rgb(2, 32, 0);">*</span>image<span style="color: rgb(2, 32, 0);">)</span> <span style="color: rgb(2, 32, 0);">{</span> NSLog<span style="color: rgb(2, 32, 0);">(</span><span style="color: rgb(191, 29, 26);">@</span><span style="color: rgb(191, 29, 26);">"图片下载成功!do something"</span><span style="color: rgb(2, 32, 0);">)</span>; <span style="color: rgb(2, 32, 0);">}</span> failure<span style="color: rgb(2, 32, 0);">:^</span><span style="color: rgb(2, 32, 0);">(</span><span style="color: rgb(64, 8, 0);">NSURLRequest</span> <span style="color: rgb(2, 32, 0);">*</span>request, <span style="color: rgb(64, 8, 0);">NSHTTPURLResponse</span> <span style="color: rgb(2, 32, 0);">*</span>response, <span style="color: rgb(64, 8, 0);">NSError</span> <span style="color: rgb(2, 32, 0);">*</span>error<span style="color: rgb(2, 32, 0);">)</span> <span style="color: rgb(2, 32, 0);">{</span> NSLog<span style="color: rgb(2, 32, 0);">(</span><span style="color: rgb(191, 29, 26);">@</span><span style="color: rgb(191, 29, 26);">图片下载成功</span>!do something"<span style="color: rgb(191, 29, 26);">"); }];</span><span style="color: rgb(191, 29, 26);">"</span><span style="color: rgb(191, 29, 26);">"</span>