简体   繁体   中英

'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];

}

Thanks in advance.

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.

A couple of big hurdles:

  • You can't just drop NSURLSession into code that uses NSURLConnection, because the delegate methods are entirely different.
  • NSURLConnection uses a per-request delegate, whereas NSURLSession uses a per-session delegate.

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.

Or just do

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

// offending line of code goes here

#pragma clang diagnostic pop

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. :-)

NSURLConnection is deprecated in iOS 9. You can use NSURLSession instead which exists since iOS 7.

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
        {
            // do something with the data 
        }];
[dataTask resume];

You also can use some 3rd party library like Alamofire .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM