如何在uiwebview(swift)中调用httpsurl?
How to call https url in uiwebview (swift)?
我在 UIWEBView 中有带自签名证书的 https url,但它不起作用。
Error is NSURLConnection/CFURLConnection HTTP load failed
(kCFStreamErrorDomainSSL, -9813)...
如何允许 swift
在 uiwebview 中进行任何认证
你有解决这个问题的办法吗?
您需要将您的证书与您的应用捆绑在一起。然后,该证书将最终受到应用程序网络组件的信任,包括您的网络视图。
我找到了一个方法,但我认为这不是一个好方法。
步骤 1------在 override func viewDidLoad(){} 部分使用 NSURLConnection
请求 = NSURL请求(URL:url)
let urlConnection:NSURLConnection = NSURLConnection(request: request, delegate: self)!
第 2 步 ------ 使用 NSURL连接委托
func connection(connection: NSURLConnection, didFailWithError error: NSError){
println("didFailWithError")
}
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
println("canAuthenticateAgainstProtectionSpace")
//return [protectionSpace.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust];
return true
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
println("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")
//if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && challenge.protectionSpace.host == "myDomain.com" {
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
println("send credential Server Trust")
let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
challenge.sender.useCredential(credential, forAuthenticationChallenge: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
println("send credential HTTP Basic")
var defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
challenge.sender.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
println("send credential NTLM")
if challenge.previousFailureCount > 0 {
//如果连续两次验证未通过,则终止, 还需要返回自定义出错界面
//handle bad credentials scenario
}
var defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
challenge.sender.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)
}
} else{
challenge.sender.performDefaultHandlingForAuthenticationChallenge!(challenge)
}
//challenge.sender.performDefaultHandlingForAuthenticationChallenge!(challenge)
//challenge.sender.continueWithoutCredentialForAuthenticationChallenge(challenge)
}
/*
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
}*/
func connection(connection: NSURLConnection, didCancelAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
println("didCancelAuthenticationChallenge")
}
/*
- (void)connection: (NSURLConnection *)connection willSendRequestForAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}*/
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){
println("-----received response");
// remake a webview call now that authentication has passed ok.
//_authenticated =YES;
//[_webloadRequest:_request];
webView.loadRequest(request)
// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
//[_urlConnectioncancel];
}
它可以工作
抱歉我的英语不好
这是 Swift 3 方法...
import UIKit
import Security
class ViewController: UIViewController, UIWebViewDelegate, NSURLConnectionDelegate {
@IBOutlet weak var webView: UIWebView!
var loadingUnvalidatedHTTPSPage:Bool!
var connection:NSURLConnection!
let path = "https://my.url.com.mx"
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
webView.scalesPageToFit = true
webView.contentMode = .scaleAspectFit
let requestObj = NSURLRequest.init(url: URL(string: path)!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10.0)
let conn:NSURLConnection = NSURLConnection(request: requestObj as URLRequest, delegate: self)!
conn.start()
self.loadingUnvalidatedHTTPSPage = true
webView.loadRequest(requestObj as URLRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK - DELEGATE METHODS WEBVIEW
private func webView(webView: UIWebView!, didFailLoadWithError error: NSError!) {
print("Webview fail with error \(error)");
if(error.domain == NSURLErrorDomain){
if (error.code == NSURLErrorServerCertificateHasBadDate || error.code == NSURLErrorServerCertificateUntrusted ||
error.code == NSURLErrorServerCertificateHasUnknownRoot || error.code == NSURLErrorServerCertificateNotYetValid) {
print("\n ---- :C ....")
}
}
}
private func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) ->Bool{
print("Webview iniciando");
if (self.loadingUnvalidatedHTTPSPage!) {
self.connection = NSURLConnection(request: request as URLRequest, delegate: self)
self.connection.start();
return false;
}
return true;
}
private func webViewDidStartLoad(webView: UIWebView!) {
print("Webview started Loading")
}
private func webViewDidFinishLoad(webView: UIWebView!) {
print("Webview did finish load")
}
// MARK - NSURLConnectionDelegate methods
func connection(_ connection: NSURLConnection, willSendRequestFor challenge: URLAuthenticationChallenge) {
let trust:SecTrust = challenge.protectionSpace.serverTrust!;
let cred:URLCredential = URLCredential(trust: trust)
challenge.sender?.use(cred, for: challenge)
}
func connection(_ connection: NSURLConnection, NSURLConnection response:URLResponse){
let requestObj:NSURLRequest = NSURLRequest(url: URL(string: path)!, cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 20.0)
self.loadingUnvalidatedHTTPSPage = false
self.webView.loadRequest(requestObj as URLRequest)
self.connection.cancel()
}
}
不要忘记在您的 .plist 文件中设置 "Allow Arbitrary Loads" = YES。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我在 UIWEBView 中有带自签名证书的 https url,但它不起作用。
Error is NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)...
如何允许 swift
在 uiwebview 中进行任何认证你有解决这个问题的办法吗?
您需要将您的证书与您的应用捆绑在一起。然后,该证书将最终受到应用程序网络组件的信任,包括您的网络视图。
我找到了一个方法,但我认为这不是一个好方法。
步骤 1------在 override func viewDidLoad(){} 部分使用 NSURLConnection 请求 = NSURL请求(URL:url)
let urlConnection:NSURLConnection = NSURLConnection(request: request, delegate: self)!
第 2 步 ------ 使用 NSURL连接委托
func connection(connection: NSURLConnection, didFailWithError error: NSError){
println("didFailWithError")
}
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
println("canAuthenticateAgainstProtectionSpace")
//return [protectionSpace.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust];
return true
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
println("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")
//if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && challenge.protectionSpace.host == "myDomain.com" {
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
println("send credential Server Trust")
let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
challenge.sender.useCredential(credential, forAuthenticationChallenge: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
println("send credential HTTP Basic")
var defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
challenge.sender.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
println("send credential NTLM")
if challenge.previousFailureCount > 0 {
//如果连续两次验证未通过,则终止, 还需要返回自定义出错界面
//handle bad credentials scenario
}
var defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
challenge.sender.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)
}
} else{
challenge.sender.performDefaultHandlingForAuthenticationChallenge!(challenge)
}
//challenge.sender.performDefaultHandlingForAuthenticationChallenge!(challenge)
//challenge.sender.continueWithoutCredentialForAuthenticationChallenge(challenge)
}
/*
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
}*/
func connection(connection: NSURLConnection, didCancelAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
println("didCancelAuthenticationChallenge")
}
/*
- (void)connection: (NSURLConnection *)connection willSendRequestForAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}*/
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){
println("-----received response");
// remake a webview call now that authentication has passed ok.
//_authenticated =YES;
//[_webloadRequest:_request];
webView.loadRequest(request)
// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
//[_urlConnectioncancel];
}
它可以工作
抱歉我的英语不好
这是 Swift 3 方法...
import UIKit
import Security
class ViewController: UIViewController, UIWebViewDelegate, NSURLConnectionDelegate {
@IBOutlet weak var webView: UIWebView!
var loadingUnvalidatedHTTPSPage:Bool!
var connection:NSURLConnection!
let path = "https://my.url.com.mx"
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
webView.scalesPageToFit = true
webView.contentMode = .scaleAspectFit
let requestObj = NSURLRequest.init(url: URL(string: path)!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10.0)
let conn:NSURLConnection = NSURLConnection(request: requestObj as URLRequest, delegate: self)!
conn.start()
self.loadingUnvalidatedHTTPSPage = true
webView.loadRequest(requestObj as URLRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK - DELEGATE METHODS WEBVIEW
private func webView(webView: UIWebView!, didFailLoadWithError error: NSError!) {
print("Webview fail with error \(error)");
if(error.domain == NSURLErrorDomain){
if (error.code == NSURLErrorServerCertificateHasBadDate || error.code == NSURLErrorServerCertificateUntrusted ||
error.code == NSURLErrorServerCertificateHasUnknownRoot || error.code == NSURLErrorServerCertificateNotYetValid) {
print("\n ---- :C ....")
}
}
}
private func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) ->Bool{
print("Webview iniciando");
if (self.loadingUnvalidatedHTTPSPage!) {
self.connection = NSURLConnection(request: request as URLRequest, delegate: self)
self.connection.start();
return false;
}
return true;
}
private func webViewDidStartLoad(webView: UIWebView!) {
print("Webview started Loading")
}
private func webViewDidFinishLoad(webView: UIWebView!) {
print("Webview did finish load")
}
// MARK - NSURLConnectionDelegate methods
func connection(_ connection: NSURLConnection, willSendRequestFor challenge: URLAuthenticationChallenge) {
let trust:SecTrust = challenge.protectionSpace.serverTrust!;
let cred:URLCredential = URLCredential(trust: trust)
challenge.sender?.use(cred, for: challenge)
}
func connection(_ connection: NSURLConnection, NSURLConnection response:URLResponse){
let requestObj:NSURLRequest = NSURLRequest(url: URL(string: path)!, cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 20.0)
self.loadingUnvalidatedHTTPSPage = false
self.webView.loadRequest(requestObj as URLRequest)
self.connection.cancel()
}
}
不要忘记在您的 .plist 文件中设置 "Allow Arbitrary Loads" = YES。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>