'initWithRequest:delegate:' 已弃用:首先在 iOS 9.0 中弃用 - 使用 NSURLSession(参见 NSURLSession.h) [英] 'initWithRequest:delegate:' is deprecated: first deprecated in iOS 9.0 - use NSURLSession(see NSURLSession.h)

查看:108
本文介绍了'initWithRequest:delegate:' 已弃用:首先在 iOS 9.0 中弃用 - 使用 NSURLSession(参见 NSURLSession.h)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是目标 C 的新手,使用 iOS 9 Xcode 7.2 并收到此警告'initWithRequest:delegate:' is deprecated: first deprecated in iOS 9.0 - use NSURLSession(see NSURLSession.h)"如何解决它.我的代码如下.

i am new in objective C and using iOS 9 Xcode 7.2 and getting this warning "'initWithRequest:delegate:' is deprecated: first deprecated in iOS 9.0 - use NSURLSession(see NSURLSession.h)" how to solve it. my code is given below.

+(id)createGetConnectionWithName:(NSString*)strConnectionName_
                       withUrl:(NSString *)pageUrl
                parameterNames:(NSArray *)arrParamNames
               parameterValues:(NSArray *)arrParamValues
                  delegate:(id)delegate
{
  NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",pageUrl]];

    NSMutableString *post =[NSMutableString string];
    for(int i=0;i<[arrParamNames count];i++)
    {
        if(i==[arrParamNames count]-1)
        {
            [post appendFormat:@"%@=%@",[arrParamNames objectAtIndex:i],
             [arrParamValues objectAtIndex:i]];
        }
        else
        {
            [post appendFormat:@"%@=%@&",[arrParamNames objectAtIndex:i],
             [arrParamValues objectAtIndex:i]];
        }
    }
    //        if(![strConnectionName_ isEqualToString:APP_AUTH_CODE_NAME])
    //            [post appendFormat:@"&Key=%@",[self getAuthCode]];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

   // conn =[[NSURLConnection alloc] initWithRequest:request delegate:self] ;
 return [[self alloc] initWithRequest:request delegate:delegate];

}

提前致谢.

推荐答案

不知道上下文,就不可能说出来.这看起来可能是 NSURLConnection 类别的一部分,在这种情况下,您必须将其转换为 NSURLSession 类别中的实例方法(使其创建数据任务),然后将整个代码库转换为使用NSURLSession.

Without knowing the context, it is impossible to say. This looks like it might be part of a category on NSURLConnection, in which case you'll have to turn it into an instance method in a category on NSURLSession (make it create a data task) and then convert your whole code base over to use NSURLSession.

几个大障碍:

  • 您不能将 NSURLSession 直接放入使用 NSURLConnection 的代码中,因为委托方法完全不同.
  • NSURLConnection 使用每个请求的委托,而 NSURLSession 使用每个会话的委托.

结果是这可能是对您的代码的重大更改,具体取决于您的委托方法的复杂程度.如果您实际上没有使用委托(或没有用它们做太多事情),那么这是一项微不足道的任务.

The result is that this is potentially a major change to your code, depending on how complicated your delegate methods are. If you aren't actually using delegates (or aren't doing much with them), then it is a trivial task.

或者只是做

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

// offending line of code goes here

#pragma clang diagnostic pop

别担心.如果 NSURLConnection 很快消失,我会感到惊讶,因为如果这样做会破坏多少代码.如果我错了,至少你会把问题搁置一段时间.:-)

and don't worry about it. I'd be surprised if NSURLConnection went away any time soon, given how much code would break if it ever did. And if I'm wrong, at least you will have punted the problem down the road for a while. :-)

这篇关于'initWithRequest:delegate:' 已弃用:首先在 iOS 9.0 中弃用 - 使用 NSURLSession(参见 NSURLSession.h)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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