网络2-AFNetworking和WebView

144
作者 庄洁元
2015.03.18 15:45 字数 1803

1.AFNNetworking 2.0

你相信你一定知道AFNNetworking,不知道你还可以看看该作者的博文,所以我就不多说关于它的强大之处了,AFNetworking 提供了比NSURLSession更高层次的抽象,这篇文章主要总结AFNNetworking 2.0的几个常用方法。

1).GET和POST请求

GET:

- (NSURLSessionDataTask *)GET:(NSString *)URLString
                   parameters:(id)parameters
                      success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                      failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure

POST:

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure

在实际的开发中,我比较习惯把第三方的代码进行一层封装,如下我们把GET和POST请求封装起来:

//GET
+ (void)getWithPath:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure{
    [self requestWithPath:path params:params success:success failure:failure method:@"GET"];
}
//POST
+ (void)postWithPath:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure{
    [self requestWithPath:path params:params success:success failure:failure method:@"POST"];
}
typedef void (^HttpSuccessBlock)(id JSON);
typedef void (^HttpFailureBlock)(NSError *error);
...省略

+ (void)requestWithPath:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure method:(NSString *)method{
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kBaseURL]];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"application/json", @"text/json", @"text/javascript", @"text/html", nil];

    if ([method  isEqual: @"GET"]) {
        [manager GET:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"fail Get!!%@",error);
            failure(error);
        }];
    }else if ([method isEqual:@"POST"]){
        [manager POST:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"POST成功:%@",responseObject);
            success(responseObject);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"fail POST!!%@",error);
            failure(error);
        }];
    }
}

你有没有注意到上面我设置了如下代码:

manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"application/json", @"text/json", @"text/javascript", @"text/html", nil];

因为AFNetworking的responseSerializer 属性只能默认只能处理特定的Content-Type,如果你想处理"text/html"等其它类型,你需要明确指定它的acceptableContentTypes。

2).多文件上传

如下,这是我写的第三方微博客户端中多图上传的实例代码:

[manager POST:@"2/statuses/upload.json"
           parameters:@{@"access_token": accseeToken,
                           @"status" : encodeStatus,
                           @"visible" : @(_intVisible),
                           @"lat" : @(_latitude),
                           @"long" : @(_longtitude)}
            constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                NSMutableArray *images = [NSMutableArray arrayWithArray:weakSelf.images];
                for (id asset in images) {
                    NSData *data = nil;
                    if ([asset isKindOfClass:[UIImage class]]) {
                        data = UIImageJPEGRepresentation(asset, 0.4);
                    }
                    if ([asset isKindOfClass:ALAsset.class]) {
                        UIImage *original = [UIImage imageWithCGImage: [[asset defaultRepresentation] fullScreenImage]];
                        data = UIImageJPEGRepresentation(original, 0.4);
                    }
                    [formData appendPartWithFileData:data name:@"pic" fileName:@"pic.jpg" mimeType:@"multipart/form-data"];
                }
            } success:^(NSURLSessionDataTask *task, id responseObject) {
                NSLog(@"发送成功");
                [self back];
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                [self showFailHUD];
            }];

3).多线程操作

如果你需要开启多个线程, 你需要使用AFHTTPRequestSerializer
,AFHTTPRequestOperation和NSOperationQueue

以下是AFNetworking的实例代码

NSMutableArray *mutableOperations = [NSMutableArray array];
for (NSURL *fileURL in filesToUpload) {
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
    }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [mutableOperations addObject:operation];
}

NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
    NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
    NSLog(@"All operations in batch complete");
}];
[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];

3).网络状态检查

网络状态检查在早期都是通过苹果官方的Reachability类进行检查,但是这个类本身存在一些问题,并且官方后来没有再更新。我们可以直接使用AFNetworking框架检测。不管使用官方提供的类还是第三方框架,用法都是类似的,通常是发送一个URL然后去检测网络状态变化,网络改变后则调用相应的网络状态改变方法。如下:

-(void)alert:(NSString *)message{
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"System Info" message:message delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
    [alertView show];
}

-(void)checkNetworkStatus{
    //创建一个用于测试的url
    NSURL *url=[NSURL URLWithString:@"http://www.apple.com"];
    AFHTTPRequestOperationManager *operationManager=[[AFHTTPRequestOperationManager alloc]initWithBaseURL:url];

    //根据不同的网络状态改变去做相应处理
    [operationManager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
                [self alert:@"2G/3G/4G Connection."];
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                [self alert:@"WiFi Connection."];
                break;
            case AFNetworkReachabilityStatusNotReachable:
                [self alert:@"Network not found."];
                break;
            default:
                [self alert:@"Unknown."];
                break;
        }
    }];
    //开始监控
    [operationManager.reachabilityManager startMonitoring];
}

2.UIWebView

UIWebView不仅能加载网络资源还可以加载本地资源,目前支持的常用的文档格式如:html、pdf、docx、txt等。

UIWebView整个使用相当简单:创建URL->创建请求->加载请求,无论是加载本地文件还是Web内容都是这三个步骤。UIWebView内容加载事件同样是通过代理通知外界,常用的代理方法如开始加载、加载完成、加载出错等,这些方法通常可以帮助开发者更好的控制请求加载过程。

加载资源:

- (void)loadRequest:(NSURLRequest *)request;

常用的属性和方法:

//重新加载(刷新) 
- (void)reload;
//停⽌止加载    
- (void)stopLoading;
//回退     
- (void)goBack;
//前进    
- (void)goForward;
//需要进⾏检测的数据类型   
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes 
//是否能回退 
@property(nonatomic,readonly,getter=canGoBack) BOOL canGoBack;
//是否能前进 
@property(nonatomic,readonly,getter=canGoForward) BOOL canGoForward; 
//是否正在加载中  
@property(nonatomic,readonly,getter=isLoading) BOOL loading;
//是否伸缩内容至适应屏幕当前尺寸 
@property(nonatomic) BOOL scalesPageToFit;

遵守UIWebViewDelegate协议,监听UIWebView的加载过程:

//开始发送请求(加载数据)时调用:
- (void)webViewDidStartLoad:(UIWebView *)webView;
//请求完毕(加载数据完毕)时调⽤:
- (void)webViewDidFinishLoad:(UIWebView *)webView;
//请求错误时调用:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
//监听UIWebView的加载过程:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

下面是一个例子:

在storyBoard中拖入如下控件:


searchBar的代理方法:

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self request:_searchBar.text];
    [searchBar resignFirstResponder];
}

加载searchBar中的请求:

-(void)request:(NSString *)urlStr{
    //创建url
    NSURL *url;

    //如果file://开头的字符串则加载bundle中的文件
    if([urlStr hasPrefix:kFILEPROTOCOL]){
        //取得文件名
        NSRange range= [urlStr rangeOfString:kFILEPROTOCOL];
        NSString *fileName=[urlStr substringFromIndex:range.length];
        url=[[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
    }else if(urlStr.length>0){
        //如果是http请求则直接打开网站
        if ([urlStr hasPrefix:@"http"]) {
            url=[NSURL URLWithString:urlStr];
        }else{//如果不符合任何协议则进行搜索
            urlStr=[NSString stringWithFormat:@"http://m.bing.com/search?q=%@",urlStr];
        }
        urlStr=[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//url编码
        url=[NSURL URLWithString:urlStr];

    }
    //创建请求
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    //加载请求页面
    [_webView loadRequest:request];
}

WebView的代理方法:

-(void)webViewDidStartLoad:(UIWebView *)webView{
    //显示网络请求加载
    [UIApplication sharedApplication].networkActivityIndicatorVisible=true;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    //隐藏网络请求加载图标
    [UIApplication sharedApplication].networkActivityIndicatorVisible=false;
    //设置按钮状态
    [self setBarButtonStatus];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"error detail:%@",error.localizedDescription);
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"系统提示" message:@"网络连接发生错误!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}

设置前进后退按钮:

-(void)setBarButtonStatus{
    if (_webView.canGoBack) {
        _barButtonBack.enabled=YES;
    }else{
        _barButtonBack.enabled=NO;
    }
    if(_webView.canGoForward){
        _barButtonForward.enabled=YES;
    }else{
        _barButtonForward.enabled=NO;
    }
}

运行结果如下:


你可以在这里下载到代码。

- (void)getAccessToken:(NSString *)requestToken
{
    [HttpTool postWithPath:@"oauth2/access_token" params:@{
       @"client_id" : kAppKey,
       @"client_secret" : kAppSecret,
       @"grant_type" : @"authorization_code",
       @"redirect_uri" : kRedirectURI,
       @"code" : requestToken
       } success:^(id JSON) {
           // 保存账号信息
           Account *account = [[Account alloc] init];
           account.accessToken = JSON[@"access_token"];
           account.uid = JSON[@"uid"];
           [[AccountTool sharedAccountTool] saveAccount:account];

           // 回到主页面
           ViewController *main = [[ViewController alloc]init];
           if (main) {
               [self presentViewController:main animated:YES completion:nil];
           }
       } failure:^(NSError *error) {


       }];
}
iOS Dev