Swift 2 - Xcode 7.0无法使用不受信任的SSL证书访问HTTPS站点 [英] Swift 2 - Xcode 7.0 Cannot Access HTTPS site with unstrusted SSL Certificate

查看:347
本文介绍了Swift 2 - Xcode 7.0无法使用不受信任的SSL证书访问HTTPS站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家,我是IOS 9 / XCODE 7 / Swift 2开发套件的初学者

Experts, I'm a Beginner in IOS 9 / XCODE 7 / Swift 2 Development Kit

我正在尝试创建一个只需路由到Web的ios应用程序应用于HTTPS协议。下面是我的代码到目前为止 ViewController.swift

I'm trying to create an ios app that simply route to Web Application in HTTPS protocol. Below is my code so far in 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.
    }
}

在构建我的应用程序时,它显示以下错误消息

While Building my App it shows the below Error Message


2015-10-01 01:05:13.879网页测试器[2947:31838]
NSURLSession / NSURLConnection HTTP加载失败
(kCFStreamErrorDomainSSL,-9807)

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 with http: //www.apple.com 它的工作正常。

and if i try building my app instead of "https://domain:8443/path/to/page" with "http://www.apple.com" its works fine.

我可以在Safari中访问我的Web应用程序并要求接受安全风险。我接受了,我可以访问我的申请。

I can access my web application in Safari and it asks for accepting the security risks. and i accept it and i can access my Application.

指导我解决我的问题,提前致谢。

Guide me to fix my issues, Thanks in advance.

推荐答案

最后我修复了它

默认情况下,Xcode会拒绝来自服务器的不受信任的自签名证书。

Xcode will reject un-trusted self signed certificates from servers by default.

我们可以覆盖这个使用 NSURLConnection 并且可以与自签名服务器通信,因为我们能够通过UIWebView无法使用的其他委托方法来控制身份验证。因此,使用 connection:didReceiveAuthenticationChallenge 我们可以对自签名服务器进行身份验证。

we can override this Using NSURLConnection and can communicate with a self-signed server, since we have the ability to control the authentication through the additional delegate methods which are not available to a UIWebView. So using connection:didReceiveAuthenticationChallenge we can authenticate against the self signed server.

参考
NSURLAuthenticationChallenge Docs ,@ Lilo Lu's 问题

References NSURLAuthenticationChallenge Docs , @Lilo Lu's Question

我通过以下步骤解决了我的问题

第1步:在我的viewController.swift的 viewDidLoad()方法中定义 NSURLConnection ,如下所示

I Resolved My Issue in below steps
Step 1 : Defined a NSURLConnection in viewDidLoad() method of my viewController.swift as follows

 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委托方法

Step 2 : used NSURLConnection delegate methods

    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)
      }
}

这是有效的!!

这篇关于Swift 2 - Xcode 7.0无法使用不受信任的SSL证书访问HTTPS站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆