2015-09-17 31 views
5

Ben IOS9 xcode yükseltilmiş ve ben, bu bölüm NSData * urlData = benim webcoder cevabı elde etme yöntemi çalışmıyor itiraz; kullanımdan kaldırıldı.IOS 9 NSURLConnection

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

Anahtarı (https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/). – Adam

+0

Neden iOS 8 için destek alıyorsunuz? – rmaddy

+0

[NSURLSession/NSURLConnection HTTP yüklemesinin iOS 9'da başarısız olması muhtemel] (http://stackoverflow.com/questions/30739473/nsurlsession-nsurlconnection-http-load-failed-on-ios-9) – Gruntcakes

cevap

12

Gerçek sorununuz NSURLConnection kullanımı değil ios9 da depriced olsa bile, burada ios9 elma kullanıyorsunuz http isteği sorunu App Transport Security (ATS) bir parçası olarak HTTP isteği kullanımını engeller Elma belgelerine

:

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

Sen projenin senin info.plist için bu tuşa ekleyerek bu atlayabilir

[NSURLSession] olarak

stack overflow link ve blog referans bağlantı ios9 ATS

2

Kullanımdan kaldırıldı şey çalışmıyor anlamına gelmez. NSURLConnection, iOS9'da hala çalışıyor.

Sorununuz HTTP kullanıyorsunuz, iOS9'da HTTPS kullanmanız gerekiyor. HTTPS'ye geçin (önerilir) veya ATS'yi devre dışı bırakın.

ATS == Apple Aktarım Güvenliği.

+0

Bunu hiç söylemedim. OP'nin kullanmaya devam edip etmediği sorunlarıyla ilgisizdir. – Gruntcakes

+0

Doğru, ve yazım hatası vardı bu yüzden geçersizdi. Sadece yorumumu ekliyordum. Bunu yakaladığın için teşekkürler. – zaph