IOS 9 NSURLConnection已弃用 [英] IOS 9 NSURLConnection deprecated

查看:133
本文介绍了IOS 9 NSURLConnection已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I upgraded to IOS9 xcode, and I do not work the method of obtaining answer of my webservice , this part NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; is deprecated.

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


推荐答案

你的实际问题不是NSURLConnection使用ios9即使它被删除也会处理它,这里你使用ios9苹果的http请求的问题不鼓励使用HTTP请求作为App Transport Security(ATS)的一部分

Your actual problem is not NSURLConnection usage ios9 will handle it even if it is depricated also , here the problem with http request you are using from ios9 apple discourages the use of HTTP request as part of the App Transport Security (ATS)

来自Apple文档:


App Transport Security(ATS)强制实施最佳做法安全应用程序与其后端之间的
连接.ATS防止意外的
泄露,提供安全的默认行为,并且易于采用;默认情况下,iOS 9和OS X中的
也是开启的v10.11。您应该尽快采用ATS
,无论您是创建新应用
还是更新现有应用。

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

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

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.

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

您可以通过将此密钥添加到项目的 info.plist 来绕过此

You can bypass this by adding this key to your info.plist of the project

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

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

这篇关于IOS 9 NSURLConnection已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆