swift – 这是否专门使用NSURLConnection来处理可转换为NSURLSes
发布时间:2020-12-14 04:54:34 所属栏目:百科 来源:网络整理
导读:我在用于测试我的服务的VM中有一个自签名证书.使用 UIWebView to view self signed websites (No private api,not NSURLConnection) – is it possible?中找到的答案,我能够编写功能强大的swift 2.0代码. Xcode 7告诉我NSURLConnection已被弃用,我应该使用NS
我在用于测试我的服务的VM中有一个自签名证书.使用
UIWebView to view self signed websites (No private api,not NSURLConnection) – is it possible?中找到的答案,我能够编写功能强大的swift 2.0代码. Xcode 7告诉我NSURLConnection已被弃用,我应该使用NSURLSession代替.我没有尝试迁移此代码,并且其他答案中描述的通常转换方案似乎都不适用.
如果我创建一个新的NSURLSession以使用我的委托方法处理身份验证质询,那么其他负载仍然会在sharedSession上发生,因此会失败. var authRequest : NSURLRequest? = nil var authenticated = false var trustedDomains = [:] // set up as necessary func webView(webView: UIWebView,shouldStartLoadWithRequest request: NSURLRequest,navigationType: UIWebViewNavigationType) -> Bool { if !authenticated { authRequest = request let urlConnection: NSURLConnection = NSURLConnection(request: request,delegate: self)! urlConnection.start() return false } else if isWebContent(request.URL!) { // write your method for this return true } return processData(request) // write your method for this } func connection(connection: NSURLConnection,willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) { if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { let challengeHost = challenge.protectionSpace.host if let _ = trustedDomains[challengeHost] { challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!),forAuthenticationChallenge: challenge) } } challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge) } func connection(connection: NSURLConnection,didReceiveResponse response: NSURLResponse) { authenticated = true connection.cancel() webview!.loadRequest(authRequest!) } 解决方法
我可以通过在第一个方法之前添加此行来抑制弃用警告.我宁愿用一个替代NSURLConnection的解决方案,但如果没有,那就必须这样做.
// hide NSURLConnection deprecation @available(iOS,deprecated=9.0) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |