我正在尝试从 URL 中获取 JSON 但无法这样做,因为发生了错误,因此响应和数据为零。
我从 URL 获取数据的代码
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();
最佳答案
使用 NSURLSessionDelegate
您需要添加适当的 session 配置
实现
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
关于ios - 此服务器的证书无效。您可能正在连接到伪装成 “domain” 的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40763967/