5

I upgraded to IOS9 xcode, and I do not work the method of obtaining answer of my webservice , this part NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; is deprecated.

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");
}
miguelzolk
  • 63
  • 1
  • 1
  • 6
  • 2
    Switch to [NSURLSession](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/). – Adam Sep 17 '15 at 15:53
  • Why are you dropping support for iOS 8 already? – rmaddy Sep 17 '15 at 15:54
  • possible duplicate of [NSURLSession/NSURLConnection HTTP load failed on iOS 9](http://stackoverflow.com/questions/30739473/nsurlsession-nsurlconnection-http-load-failed-on-ios-9) – Gruntcakes Sep 17 '15 at 16:22
  • this link http://stackoverflow.com/questions/30739473/nsurlsession-nsurlconnection-http-load-failed-on-ios-9 fixed my problem, thank guys! – miguelzolk Sep 21 '15 at 19:41

2 Answers2

12

Your actual problem is not NSURLConnection usage ios9 will handle it even if it is depricated also , here the problem with http request you are using from ios9 apple discourages the use of HTTP request as part of the App Transport Security (ATS)

From Apple documentation:

"App Transport Security (ATS) enforces best practices in the secure connections between an app and its back end. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt; it is also on by default in iOS 9 and OS X v10.11. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one.

If you’re developing a new app, you should use HTTPS exclusively. If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible. In addition, your communication through higher-level APIs needs to be encrypted using TLS version 1.2 with forward secrecy.

If you try to make a connection that doesn't follow this requirement, an error is thrown. If your app needs to make a request to an insecure domain, you have to specify this domain in your app's Info.plist file."

You can bypass this by adding this key to your info.plist of the project

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

stack overflow link and blog reference link ios9 ATS

Community
  • 1
  • 1
Anshad Rasheed
  • 2,526
  • 1
  • 14
  • 32
2

Deprecated doesn't mean something doesn't work. NSURLConnection still works in iOS9.

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

ATS == Apple Transport Security.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • I never said it did. Whether the OP continues to use it or not is irrelevant to their problem. – Gruntcakes Oct 09 '15 at 20:22
  • True, and there was a typo so it was incorret. I was just adding my comment. Thanks for catching that. – zaph Oct 09 '15 at 20:42