此服务器的证书无效。您可能正在连接到假装为“域”的服务器

时间:2016-11-23 11:59:34

标签: ios swift ssl ssh server

我正在尝试从URL获取JSON,但由于发生错误而无法执行此操作,因此响应和数据为零。

我从网址

获取数据的代码
 loadData(urlString: String, completion : @escaping (Player,Player) -> Void) -> Void {
    let config = URLSessionConfiguration.default

    let session = URLSession(configuration: config)

    let url = URL(string: urlString)
    let task = session.dataTask(with: url!){data,response,error in
        if error != nil{
            print("problem in loading data");
            print(error.debugDescription);
        } else{
            let players = self.parseJson(data: data as NSData?)

            let priority = DispatchQueue.global(qos: .userInitiated)
            priority.async {
                DispatchQueue.main.async {
                    completion(players.0, players.1);
                }
            }
        }
    }
    task.resume();

错误的Debug描述如下,有关info.plist中的App Transport Security的详细信息 - Image showing info.plist and error.debugDescription

用于从JSON URL获取数据的代码图像。 enter image description here

1 个答案:

答案 0 :(得分:0)

使用NSURLSessionDelegate

enter image description here

您需要添加正确的会话配置

enter image description here 实行  - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler

enter image description here