UIWebView 不加载 HTTPS 页面:错误域=NSURLErrorDomain 代码=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)"

UIWebView does not load HTTPS Page: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)"

注意:此问题仍未得到解答!

我使用 UIWebView 加载以下网址:

https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655

https://buchung.salonmeister.de/place/#offer-details-page?id=907599&venueId=301655

http://feratel.lueneburger-heide.de/lhg/de/accommodation/detail/LUH/8634e147-e13d-40f5-8954-2ac40cfea2a7/romantik_hotel_bergström?customHeader=true

http://feratel.lueneburger-heide.de/lhg/de/accommodation/detail/LUH/8af6d1fd-af6d-4765-8025-9eb8fa05ea42/hotel%20undeloher%20hof?customHeader=true

请注意,上面的 url 不是我的 url,所以我无法更改它们的内容。

尝试加载时,我从 func webView(webView: UIWebView, didFailLoadWithError error: NSError) 收到以下错误:

Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x7ff7d0fd6f20 {NSErrorFailingURLStringKey=https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655, NSErrorFailingURLKey=https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655}

我在 iOS7 和 iOS8 上测试了下面的代码。 Mobile Safari 加载此页面没有任何问题,但在我的 UIWebView 中我什么也没看到。

这是我使用的代码:

class ViewController: UIViewController, UIWebViewDelegate, NSURLConnectionDataDelegate {
  @IBOutlet weak var webView: UIWebView!
  var request: NSURLRequest!
  var urlString: String!
  private var isDone: Bool = false
  private var failedRequest: NSURLRequest!

  override func viewDidLoad() {
    super.viewDidLoad()

    urlString = "https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655"
    request = NSURLRequest(URL: NSURL(string: urlString)!)
    webView.delegate = self
  }

  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.webView.loadRequest(self.request)
  }

  // MARK: UIWebViewDelegate

  func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    println("shouldStartLoadWithRequest()")

    if !isDone {
      isDone = false

      println("shouldStartLoadWithRequest() 111")
      failedRequest = request
      webView.stopLoading()
      var connection = NSURLConnection(request: request, delegate: self, startImmediately: true)
      connection!.start()
      //      NSURLConnection(request: request, delegate: self)
      return false
    }
    println("shouldStartLoadWithRequest() -----------------------")
    return true
  }

  func webViewDidStartLoad(webView: UIWebView) {
    println("webViewDidStartLoad()")
  }

  func webViewDidFinishLoad(aWebView: UIWebView) {
    println("webViewDidFinishLoad()")
  }

  func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
    println("webView(): didFailLoadWithError(): \(error)")
  }

  // MARK: NSURLConnectionDataDelegate

  func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
    println("connection willSendRequestForAuthenticationChallenge")

    if challenge.previousFailureCount == 0 {
      self.isDone = true
      println("x1")
      let credential: NSURLCredential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
      challenge.sender.useCredential(credential, forAuthenticationChallenge: challenge)

    }
    else {
      println("x2")
      challenge.sender.cancelAuthenticationChallenge(challenge)
    }
  }

  func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
    println("connection didReceiveResponse")
    self.isDone = true

    connection.cancel()
    self.webView.loadRequest(self.failedRequest)
  }

  func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool {
    println("connection canAuthenticateAgainstProtectionSpace")
    return protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
  }
}

我在这里用上面的代码创建了一个示例项目:https://github.com/confile/UIWebView-https-url

如何让我的 UIWebView 正确加载 url?

我运行你的代码。大约 50% 的时间它工作正常,其他时间显示 -999 错误。

根据 URL Loading System Error Codes in Apple's Foundation Constants Reference,该错误代码为 NSURLErrorCancelled。所以某些东西正在取消页面加载。

我打印了收到的页面内容,使用的是:

println(aWebView.stringByEvaluatingJavaScriptFromString("document.body.innerHTML"));

我看到页面中的 Javascript 包含以下内容:

if (mobilePath) {
   document.body.style.display = 'none';
   document.location = mobilePath;  
}

我的猜测是这个 Javascript 内容一加载就开始 运行。这会导致争用情况,有时页面会立即重定向到另一个 document.location,因此第一个页面加载会在完全加载之前被取消。

您应该更改服务器,使其不发送此 Javascript。

或者,如果您只想像正常网站一样加载网站,那么您可以让 UIWebView 为您处理重定向。对于这个站点,我通过将用户代理设置为假装为移动版 Safari 使其正常工作。我还删除了 shouldStartLoadWithRequest,因为它只会干扰事情。

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    var request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
    var userAgent = ["UserAgent": "mozilla/5.0 (iphone; cpu iphone os 7_0_2 like mac os x) applewebkit/537.51.1 (khtml, like gecko) version/7.0 mobile/11a501 safari/9537.53"]
    NSUserDefaults.standardUserDefaults().registerDefaults(userAgent as [NSObject : AnyObject])

    self.webView.loadRequest(request)
  }

如果我没有伪装成移动版 Safari 的代码,那么页面内容会加载,但它不会执行正确的操作。它只是停在一个微调器上。我假设该网站正在发送不同的 Javascript,具体取决于它认为正在与之通信的渲染引擎。如果您不指定任何用户代理,那么您将受制于他们的网页设计师在指定默认行为方面所做的一切。