2015-09-17 12 views
5

ho aggiornato a IOS9 Xcode, e io non lavoro il metodo per ottenere risposta del mio webservice, questa parte NSData * URLData = [NSURLConnection sendSynchronousRequest: Richiesta returningResponse: & risposta di errore: Errore di &]; è deprecato.IOS 9 NSURLConnection deprecato

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"); 
} 
+2

Passa a [NSURLSession] (https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/). – Adam

+0

Perché stai già lasciando cadere il supporto per iOS 8? – rmaddy

+0

possibile duplicato di [NSURLSession/NSURLConnection carico HTTP non riuscito su iOS 9] (http://stackoverflow.com/questions/30739473/nsurlsession-nsurlconnection-http-load-failed-on-ios-9) – Gruntcakes

risposta

12

Il tuo problema reale non è l'utilizzo NSURLConnection ios9 lo gestirà anche se è stato rimosso anche, in questo caso il problema con la richiesta http che stai utilizzando da Apple ios9 scoraggia l'utilizzo della richiesta HTTP come parte di App Transport Security (ATS)

da documentazione di Apple:

"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."

è possibile ignorare questo con l'aggiunta di questa chiave al tuo info.plist del progetto

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

stack overflow link e riferimento blog Link ios9 ATS

2

obsolete non significa qualcosa non funziona. NSURLConnection funziona ancora su iOS9.

Il problema è che si sta utilizzando HTTP, in iOS9 si suppone di utilizzare HTTPS. Passare a HTTPS (consigliato) o disabilitare ATS.

ATS == Sicurezza trasporto Apple.

+0

Non ho mai detto che fosse così. Se il PO continua a usarlo o no è irrilevante per il loro problema. – Gruntcakes

+0

Vero, e c'era un errore di battitura quindi era incorret. Stavo solo aggiungendo il mio commento. Grazie per averlo capito. – zaph