2015-09-17 30 views
5

I przeniesieni do IOS9 Xcode, a ja nie działają metody uzyskiwania odpowiedzi na moje usługa ta część NSData * urlData = [NSURLConnection sendSynchronousRequest: wniosek returningResponse: & odpowiedź błąd: & error]; jest przestarzałe.IOS 9 NSURLConnection przestarzałe

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

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

+0

Dlaczego rezygnujesz z obsługi systemu iOS 8? – rmaddy

+0

możliwy duplikat obciążenia HTTP NSURLSession/NSURLConnection na iOS 9] (http://stackoverflow.com/questions/30739473/nsurlsession-nsurlconnection-http-load-failed-on-ios-9) – Gruntcakes

Odpowiedz

12

Twoim rzeczywistym problemem nie jest użycie NSURLConnection. Ios9 poradzi sobie z nim, nawet jeśli jest również zdeponowany, tutaj problem z żądaniem http, którego używasz z ios9 apple, zniechęca do korzystania z żądania HTTP w ramach App Transport Security (ATS)

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

można ominąć ten dodając ten klucz do info.plist projektu

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

stack overflow link i odniesienie strony łącza ios9 ATS

2

Nieaktualne nie znaczy coś nie działa. NSURLConnection nadal działa w iOS9.

Problem polega na tym, że korzystasz z protokołu HTTP, w iOS9 masz używać protokołu HTTPS. Przełącz na HTTPS (zalecane) lub wyłącz ATS.

ATS == Apple Transport Security.

+0

Nigdy nie powiedziałem, że tak. To, czy OP będzie nadal z niego korzystać, czy nie, nie ma związku z ich problemem. – Gruntcakes

+0

To prawda, a był literówka, więc był wcielony. Właśnie dodałem mój komentarz. Dzięki za złapanie tego. – zaph

Powiązane problemy