IOS 9 NSURLConnection deprecated

I upgraded to IOS9 xcode, and I do not use the method of receiving a response to my web service, this part NSData * urlData = [NSURLConnection sendSynchronousRequest: request returnResponse: & response error: & error]; outdated.

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/index.html#//apple_ref/doc/uid/TP40003755

NSString *post =[[NSString alloc] initWithFormat:@"lang=%@&type=ssss",@"en"]; NSURL *url=[NSURL URLWithString:@"http://www.xxxxxxxxxx.com/xxxxxxx/ws/get_xxxxx.php"]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSLog(@"esta es la url: %@", postData); 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/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSError *error = [[NSError alloc] init]; NSHTTPURLResponse *response = nil; NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if ([response statusCode] >=200 && [response statusCode] <300) { NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error]; NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue]; if(success == 1) { if (self.lugares != nil) { self.lugares = nil; } self.lugares = [[NSMutableArray alloc] initWithArray:[jsonData objectForKey:@"lugares"]]; } else { NSLog(@"no hay datos :C"); } } else { NSLog(@"No encontrado"); } 
+5
source share
2 answers

Your actual problem is not using NSURLConnection. ios9 will handle it even if it is also deprived. Here's the problem with the HTTP request that you use from ios9 apple, discourages using the HTTP request as part of application transport security (ATS)

From the Apple documentation:

“Application Transport Security (ATS) provides the best practice for secure connections between the application and its rear end. ATS prevents accidental disclosure of information, provides secure default behavior and is easily adaptable; It is also included by default in iOS 9 and OS X v10.11. You must accept the ATS as soon as possible, whether you are creating a new application or updating an existing one.

If you are developing a new application, you should use only HTTPS. If you have an existing application, you should use HTTPS as much as you can right now, and create a migration plan for the rest of your application as soon as possible. In addition, your communication through a higher level API must be encrypted using TLS version 1.2 with direct secrecy.

If you try to establish a connection that does not meet this requirement, an error occurs. If your application needs to make a request to an insecure domain, you must specify this domain in your application. Info.plist file. "

You can get around this by adding this key to your project info.plist

 <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 

link and link to ios9 ATS blog link

+12
source

Outdated does not mean that something is not working. NSURLConnection still works on iOS9.

Your problem is that you are using HTTP, in iOS9 you must use HTTPS. Switch to HTTPS (recommended) or disable ATS.

ATS == Apple Transport Security.

+2
source

All Articles