各位高手,我是IOS 9/XCODE 7/Swift 2开发套件的新手
我正在尝试创建一个 ios 应用程序,它可以简单地路由到 HTTPS 协议(protocol)中的 Web 应用程序。以下是我目前在 ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet var myWebView: UIWebView!
/**
* Function to Display the Web Application initial URL
*/
func loadAppURL(){
let siteAddress = "https://domain:8443/path/to/page"
let url = NSURL (string: siteAddress)
let urlRequest = NSURLRequest(URL: url!)
myWebView.loadRequest(urlRequest)
}
override func viewDidLoad() {
super.viewDidLoad()
loadAppURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
在构建我的应用程序时显示以下错误消息
2015-10-01 01:05:13.879 Web Page Tester[2947:31838] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
如果我尝试用“https://domain:8443/path/to/page”构建我的应用程序而不是“http://www.apple.com”,它工作正常。
我可以在 Safari 中访问我的 Web 应用程序,它要求接受安全风险。我接受它并且我可以访问我的应用程序。
指导我解决我的问题,在此先感谢。
最佳答案
终于解决了
默认情况下,Xcode 将拒绝来自服务器的不受信任的自签名证书。
我们可以使用 NSURLConnection
覆盖它,并且可以与自签名服务器通信,因为我们能够通过 UIWebView 无法使用的其他委托(delegate)方法来控制身份验证。因此,使用 connection:didReceiveAuthenticationChallenge
我们可以针对自签名服务器进行身份验证。
引用文献 NSURLAuthenticationChallenge Docs , @Lilo Lu的Question
我通过以下步骤解决了我的问题
第 1 步: 在我的 viewController.swift 的 viewDidLoad()
方法中定义了一个 NSURLConnection
,如下所示
override func viewDidLoad() {
super.viewDidLoad()
let siteAddress = "https://domain:8443/path/to/page"
let url = NSURL (string: siteAddress)
let urlRequest = NSURLRequest(URL: url!)
let urlConnection:NSURLConnection = NSURLConnection(request: request, delegate: self)!
myWebView.loadRequest(urlRequest)
}
第 2 步:使用 NSURLConnection 委托(delegate)方法
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
print("canAuthenticateAgainstProtectionSpace method Returning True")
return true
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
print("send credential Server Trust")
let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
print("send credential HTTP Basic")
let defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
challenge.sender!.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
print("send credential NTLM")
} else{
challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
}
}
成功了!!
关于ios - Swift 2 - Xcode 7.0 无法使用不受信任的 SSL 证书访问 HTTPS 站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32876602/