2015-09-17 30 views
5

Tôi đã nâng cấp lên IOS9 xcode và tôi không làm việc theo phương thức lấy câu trả lời của dịch vụ web của tôi, phần này NSData * urlData = [NSURLConnection sendSynchronousRequest: request returnResponse: & lỗi phản hồi: & lỗi]; không được chấp nhận.IOS 9 NSURLConnection không dùng nữa

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

Đổi thành [NSURLSession] (https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/). – Adam

+0

Tại sao bạn lại bỏ hỗ trợ cho iOS 8? – rmaddy

+0

có thể trùng lặp với tải HTTP [NSURLSession/NSURLConnection không thành công trên iOS 9] (http://stackoverflow.com/questions/30739473/nsurlsession-nsurlconnection-http-load-failed-on-ios-9) – Gruntcakes

Trả lời

12

Vấn đề thực tế của bạn không phải là NSURLKết quả sử dụng ios9 sẽ xử lý ngay cả khi nó được giải quyết, ở đây vấn đề với yêu cầu http bạn đang sử dụng từ ios9 táo không khuyến khích sử dụng yêu cầu HTTP như một phần của Bảo mật chuyển tải ứng dụng (ATS)

Từ tài liệu của 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."

bạn có thể bỏ qua điều này bằng cách thêm phím này để info.plist bạn của dự án

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

stack overflow link và blog tham khảo liên kết ios9 ATS

2

Deprecated không có nghĩa là một cái gì đó không hoạt động. NSURLConnection vẫn hoạt động trong iOS9.

Vấn đề của bạn là bạn đang sử dụng HTTP, trong iOS9 bạn có nghĩa vụ phải sử dụng HTTPS. Chuyển sang HTTPS (được khuyến nghị) hoặc tắt ATS.

ATS == Bảo ​​mật giao thông của Apple.

+0

Tôi chưa từng nói điều đó. OP có tiếp tục sử dụng hay không không liên quan đến vấn đề của họ. – Gruntcakes

+0

Đúng, và có lỗi đánh máy vì vậy nó không phù hợp. Tôi vừa thêm bình luận của mình. Cảm ơn vì đã bắt được điều đó. – zaph

Các vấn đề liên quan