NSURLConnection

NSURLConnection 网络连接问题

 需要的类:

    1.NSURL: 请求地址
     2.NSURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSURL对象,请求方法、请求头、请求体
     3.NSMutableURLRequest:NSURLRequest的子类
     4.NSURLConnection:负责发送请求,建立客户端和服务器的连接。发送NSURLRequest的数据给服务器,并收集来自服务器的响应数据


   步骤:(1)创建一个对象NSURL,设置请求路径
          (2).传入NSURL,创建NSURLRuquest对象,设置请求头和请求题
          (3).使用NSURLConnection发送NSURLRequest(发送请求)
  @property(nonatomic,strong)NSMutableData *requestData;

在Info.plist中设置网络


//(1).设置请求路径
     NSString *urlStr=[NSString stringWithFormat:@"http://mobby-ipad.oss-cn-beijing.aliyuncs.com/appversion/version.json"];
     NSURL *url=[NSURL URLWithString:urlStr];
通过代理实现异步加载,遵循代理方法:

//设置请求超时
    NSMutableURLRequest *mutRequest = [NSMutableURLRequest requestWithURL:url];
    mutRequest.timeoutInterval = 5.0;
    
    //发送请求,设置代理异步发送请求
    NSURLConnection *conn=[NSURLConnection connectionWithRequest:mutRequest delegate:self];
    
    [conn start];

代理方法:

//当接收到服务器的响应时会调用
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
   NSLog(@"接收到服务器的响应");
    self.requestData = [NSMutableData data];
    
}
//当接收到服务器的数据时候会调用(每次只传递一部分数据)
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"接收到服务器的数据");
    [self.requestData appendData:data];

}//当数据的服务器加载结束后会调用
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"服务器加载结束后");
    //处理服务器返回的所有数据
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:self.requestData options:NSJSONReadingMutableLeaves error:nil];

}//加载失败的时候调用
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
    NSLog(@"加载失败");
}
第二种方法:异步加载,通过队列的方式进行加载数据

//(1).设置请求路径
     NSString *urlStr=[NSString stringWithFormat:@"http://moy-ipad.oss-cn-beijing.aliycs.com/appversion/version.json"];
     NSURL *url=[NSURL URLWithString:urlStr];
//(2).创建请求对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
//(3).发送请求
//    (3.1),发送同步请求,在主线程中执行(一直在等待服务器返回数据,这行代码会卡住,如果主线程没有返回数据,主线程会卡住不会继续执行)
//   NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
//    (3.1).发送异步请求,创建一个队列,(默认添加到该队列中的任务异步执行)
    NSOperationQueue *queue = [NSOperationQueue mainQueue];  //获取一个主队列
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSLog(@"--block回调数据--%@---%d", [NSThread currentThread],data.length);
        
        if (data) {
           NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
           
            for (NSDictionary *dic in array) {
                if ([[dic objectForKey:@"appName"]isEqualToString:@"PreschoolFamilyClient"]) {
                    NSString *string  = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
                    if ([string compare:[dic objectForKey:@"appVersion"]] == NSOrderedAscending) {
                        UIAlertView *al = [[UIAlertView alloc]initWithTitle:@"版本检测" message:@"有新的版本,快点去体验吧!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                        al.delegate = self;
                        [al show];

                    }
                }
                
            }
        }
        
          }];
    NSLog(@"请求发送完毕");
    

版本更新

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSURL *url = [NSURL URLWithString:@"http://h.moy.cn"];
    [[UIApplication sharedApplication]openURL:url];
}






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值