【问题标题】:IOS 9 NSURLConnection deprecatedIOS 9 NSURLConnection 已弃用
【发布时间】:2015-09-17 15:49:58
【问题描述】:

我升级到IOS9 xcode,我的webservice获取答案的方法没有工作,这部分 NSData *urlData=[NSURLConnection sendSynchronousRequest:request returnedResponse:&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");
}

【问题讨论】:

标签: ios objective-c web-services


【解决方案1】:

您的实际问题不是 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>

stack overflow link 和博客参考链接 ios9 ATS

【讨论】:

    【解决方案2】:

    不推荐使用并不意味着某些东西不起作用。 NSURLConnection 在 iOS9 中仍然有效。

    您的问题是您使用的是 HTTP,在 iOS9 中您应该使用 HTTPS。切换到 HTTPS(推荐)或禁用 ATS。

    ATS == Apple 传输安全性。

    【讨论】:

    • 我从来没有说过。 OP 是否继续使用它与他们的问题无关。
    • 是的,而且有一个错字,所以它是不正确的。我只是添加我的评论。感谢您了解这一点。
    猜你喜欢
    • 1970-01-01
    • 2015-12-15
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2015-12-17
    • 2019-02-17
    相关资源
    最近更新 更多