IOS 9 NSURLConnection已弃用

mig*_*mgo 5 web-services objective-c ios

我升级到IOS9 xcode,我不工作获取我的webservice答案的方法,这部分NSData*urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 已弃用.

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");
}
Run Code Online (Sandbox Code Playgroud)

Ans*_*eed 12

你的实际问题不是NSURLConnection使用ios9将处理它,即使它也被删除,这里你使用ios9苹果的http请求的问题不鼓励使用HTTP请求作为应用程序传输安全(ATS)的一部分

来自Apple文档:

"应用程序传输安全(ATS)在应用程序及其后端之间的安全连接中实施最佳实践.ATS防止意外泄露,提供安全的默认行为,并且易于采用;默认情况下,它也在iOS 9和OS X中启用v10.11.无论您是创建新应用程序还是更新现有应用程序,都应尽快采用ATS.

如果您正在开发新应用,则应该专门使用HTTPS.如果您有现有应用,则应尽可能多地使用HTTPS,并创建一个计划,以便尽快迁移其余应用.此外,您通过更高级别的API进行的通信需要使用具有前向保密性的TLS 1.2版进行加密.

如果尝试建立不符合此要求的连接,则会引发错误.如果您的应用需要向不安全的域发出请求,则必须在应用的Info.plist文件中指定此域."

您可以通过将此键添加到info.plist项目中来绕过此操作

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)

堆栈溢出链接和博客参考链接 ios9 ATS