spi*_*her 5 ssl self-signed deprecated nsurlconnection swift
我在用于测试我的服务的VM中有一个自签名证书.使用UIWebView中的答案来查看自签名网站(没有私人API,而不是NSURLConnection) - 是否可能?我能够编写功能强大的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!)
}
Run Code Online (Sandbox Code Playgroud)
我能够通过在第一个方法之前添加此行来抑制弃用警告。我更喜欢一个替代 NSURLConnection 的解决方案,但如果没有的话,就必须这样做。
// hide NSURLConnection deprecation
@available(iOS, deprecated=9.0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
205 次 |
| 最近记录: |