0

I am new to Mac application development and i am working on fixing the deprecation issues present in the application. I am using XCode 11.3 in Mojave MacOS 10.14 and Our application contains the following line of code

[NSURLConnection sendSynchronousRequest:request returningResonse:response error:Error]

A warning message getting displayed as sendSynchronousRequest is deprecated and it is suggesting to use [NSURLSession dataTaskWithRequest:completionHandler:] .

so i have implemented the following code

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue    mainQueue]];
NSURL * url = [NSURL URLWithString:@"MyURLHere"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];    
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                      NSLog(@"Response:%@ %@\n", response, error);
                                      if(error == nil)
                                      {
                                          NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                      }
                                  }];
[dataTask resume];

i found that data is being returned as nil always. When i debugged the code, i found that even [NSURLSessionConfiguration defaultSessionConfiguration] is being returned as nil. Can you please suggest what needs to be done in order to fix these issues?

Willeke
  • 14,578
  • 4
  • 19
  • 47
Kamal
  • 453
  • 1
  • 10
  • 22
  • When i declare NSURLSession *session = [NSURLSession sharedSession]; and check the value of sesion then it is being returned as nil .. What is causing to return nil always for this line of code? – Kamal Jul 22 '20 at 06:11
  • 1
    @Rob Yes i am using connection that starts with https:// only – Kamal Jul 22 '20 at 06:24
  • @Rob Even i have checked the following simple code but still the data is returned as nil NSURL *URL = [NSURL URLWithString:@"http://example.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { // ... }]; [task resume]; – Kamal Jul 22 '20 at 07:01
  • There’s not enough here to answer your question. And you haven’t answered the clarification questions (have you turned on outbound connections in the project capabilities tab? What were `response` and `error` objects? Where are you printing the `data`?) – Rob Jul 22 '20 at 07:58
  • @Rob I am new to this development process so i am in search of these settings. Can you please tell me the exact location where can i find the outbound connections ? – Kamal Jul 22 '20 at 08:07
  • I have [moved this discussion to chat](https://chat.stackoverflow.com/rooms/218366/discussion-between-rob-and-kamal). – Rob Jul 22 '20 at 15:43

0 Answers0