2

我想在 iOS 模拟器上访问公司内部 https 的页面。这个页面可以在 safari 上访问。但是 WKWebView 无法访问。程序说以下错误。

An SSL error has occurred and a secure connection to the server cannot be made.

TLS 版本为 TLSv1.2。

如果我设置 NSAllowsArbitraryLoads,我可以访问。但我认为这种方式并不好。

我的代码如下。

//
//  ViewController.swift
//  TestClient
//
//  Created by 平塚 俊輔 on 2015/12/07.
//  Copyright © 2015年 平塚 俊輔. All rights reserved.
//

//1.WebKit Frameworkをimportする
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

    //2.WKWebviewの宣言!
    var _webkitview: WKWebView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //3.WebKitのインスタンス作成!
        self._webkitview = WKWebView()

        //4.ここでWebKitをviewに紐付け
        self.view = self._webkitview!
        self._webkitview!.navigationDelegate = self
        //5.URL作って、表示させる!
        var url = NSURL(string:"https:/******")
        var req = NSURLRequest(URL:url!)
        self._webkitview!.loadRequest(req)
    }

    // MARK: WKNavigationDelegate
    func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        NSLog("Start")
    }

    func webView(webView: WKWebView!, didFailNavigation navigation: WKNavigation!, withError error: NSError!) {
        NSLog("Failed Navigation %@", error.localizedDescription)
    }

    func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) {
        // Finish navigation
        NSLog("Finish Navigation")
        NSLog("Title:%@ URL:%@", webView.title!, webView.URL!)

        // Run Javascript(For local)
//        webView.evaluateJavaScript("var el=document.getElementById('user');el.style.backgroundColor='yellow';", nil)
    }

    func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
        print(error)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

这是什么问题?

顺便说一句,我可以在真实设备上访问。我无法仅在模拟器上访问。

4

2 回答 2

4

应用传输安全不仅仅涉及 HTTPS (TLS)。它还涉及可能允许也可能不允许的特定类型的密码和证书。

您将在此处找到详细信息:

https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW35

使用 ATS 连接的要求

使用 App Transport Security (ATS) 的 Web 服务连接要求涉及服务器、连接密码和证书,如下所示:

  • 证书必须使用以下类型的密钥之一进行签名:
    • 安全散列算法 2 (SHA-2) 密钥,摘要长度至少为 256(即 SHA-256 或更大)
    • 椭圆曲线加密 (ECC) 密钥,大小至少为 256 位
    • 长度至少为 2048 位的 Rivest-Shamir-Adleman (RSA) 密钥

无效的证书会导致硬故障并且没有连接。

  • 以下连接密码支持前向保密 (FS) 并与 ATS 一起使用:

TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA

“好”选项是重新配置您的服务器以使用适当的密码和证书。否则,您可以对所有站点或仅针对特定域和/或子域禁用全局或更细粒度的检查。异常“越严格”越好。

于 2015-12-07T12:36:12.107 回答
0

在 iOS9 中,Apple 引入了 App Transport Security (ATS),它可以阻止来自 iOS 应用程序的所有不安全的 HTTP 流量。

要禁用 ATS,您可以按照以下快速步骤操作:右键单击 Info.plist 并选择view as> source code,然后添加以下行:

<key>NSAppTransportSecurity</key>
<dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>

但这不是推荐的方法。您必须向要访问的域添加例外,例如:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>

你可以在这个问题中找到你需要的所有信息

于 2015-12-07T12:28:37.777 回答