Objective-C - 'sendSynchronousRequest:returningResponse:error:'已弃用:在iOS 9.0中首先弃用 [英] Objective-C - 'sendSynchronousRequest:returningResponse:error:' is deprecated: first deprecated in iOS 9.0

查看:2554
本文介绍了Objective-C - 'sendSynchronousRequest:returningResponse:error:'已弃用:在iOS 9.0中首先弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-(NSArray *)deviceCheck:(NSString *)device
    {
        NSString *deviceRequestString = [NSString stringWithFormat:@"%@?device=%@",webservice,device];
        NSURL *JSONURL = [NSURL URLWithString:deviceRequestString];
        NSURLResponse* response = nil;
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];

        NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
        if(data == nil)
            return nil;
        NSError *myError;
        NSArray *tableArray = [[NSArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
        return tableArray;
    }

但我一直收到此警告:

sendSynchronousRequest:returningResponse:error:' is deprecated: first deprecated in iOS 9.0 - Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h

此行:

NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

I尝试更改为以下内容:

I tried changing to to the following:

NSData* data = [NSURLSession dataTaskWithRequest:request];

NSData* data = [NSURLSession dataTaskWithRequest:request returningResponse:&response error:nil];

两者都给我错误说:

没有已知的类方法

请帮助

推荐答案

使用 NSURLSession ,你的代码可能是这样的

With NSURLSession,your code may like this

-(void)deviceCheck:(NSString *)device Completetion:(void (^) (NSArray * result,NSError * error))completion{
NSString *deviceRequestString = [NSString stringWithFormat:@"%@?device=%@",webservice,device];
NSURL *JSONURL = [NSURL URLWithString:deviceRequestString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
NSURLSessionDataTask * dataTask = [
      [NSURLSession sharedSession]
      dataTaskWithRequest:request
      completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
          if(data == nil) {
              completion(nil,error);
              return;
          }
          NSError *myError;
          NSArray *tableArray = [[NSArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
          completion(tableArray,myError);
      }
];
[dataTask resume];
}

然后当你使用它时

[self deviceCheck:@"123" Completetion:^(NSArray *result, NSError *error) {
   //Here use result,and check the error
}];

注意,这种方法是异步的

Note,this method is async

这篇关于Objective-C - 'sendSynchronousRequest:returningResponse:error:'已弃用:在iOS 9.0中首先弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆