ios - URLSession 委托(delegate)不适用于 swift 4.2

标签 ios swift urlsession

此代码在 Swift 版本 3 上运行良好,我无法使其在 Swift 4 上运行

func rest() {
        let path = "https://localhost:8443/someservice"
        let request = NSMutableURLRequest(URL: NSURL(string: path)!)
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue())
        let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            let json:JSON = JSON(data: data!)
            if let c = json["content"].string {
                print(c)
            }
        })
        task.resume()
    }
func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
        completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
    }

最佳答案

您可能需要最新的语法

func urlSession(_ session: URLSession, 
                task: URLSessionTask, 
          didReceive challenge: URLAuthenticationChallenge, 
   completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

关于ios - URLSession 委托(delegate)不适用于 swift 4.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54246474/

相关文章:

ios - 我们可以从Apple Watch实例化MFMessageComposeViewController类的类吗

swift - swift 启动 segue 的问题

arrays - 如何在 Swift 中对 NSDate 进行排序

swift - NSMutableURLRequest forHTTPHeaderField key 未正确设置

ios - 如何获取json数据并通过segue传递它

Swift 4 URLSession 422 响应没有错误

ios - 如何在 iOS 中实现画笔描边

ios - 在 deviceready 上检查 navigator.globalization 在 iOS 上返回 undefined

android - 如何创建多平台本地化解决方案?

ios - 如何在 Cell 的 UIImageView 上添加 mask - Swift