Swift UIWebView不加载HTTPS页面:错误域=NSURLErrorDomain代码=-999";操作无法’;不可能完成。(NSURlerorDomain错误-999)。”;

Swift UIWebView不加载HTTPS页面:错误域=NSURLErrorDomain代码=-999";操作无法’;不可能完成。(NSURlerorDomain错误-999)。”;,swift,https,uiwebview,Swift,Https,Uiwebview,注意:这个问题仍然没有答案 我使用UIWebView加载以下URL: 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

注意:这个问题仍然没有答案

我使用
UIWebView
加载以下URL:

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:NSError)收到以下错误:

Error Domain=NSURERRORDOMAIN Code=-999“该操作无法完成。(NSURERRORDOMAIN错误-999)。”UserInfo=0x7ff7d0fd6f20{NSERRORFAILINGURSTRINGKEY=,NSERRORFAILINGURKEY=}

我在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
  }
}
我在这里用上面的代码创建了一个示例项目:


如何获取我的
UIWebView
正确加载url?

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

根据,该错误代码已取消。所以有东西取消了页面加载

我使用以下方法打印收到的页面内容:

println(aWebView.stringByEvaluatingJavaScriptFromString("document.body.innerHTML"));
我看到页面中的Javascript包含以下内容:

if (mobilePath) {
   document.body.style.display = 'none';
   document.location = mobilePath;  
}
我的猜测是,一旦加载内容,这个Javascript就会开始运行。这会导致竞争情况,有时页面会立即重定向到不同的
文档位置
,因此第一次页面加载会在完全加载之前取消

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

或者,如果您只想像普通网站一样加载网站,那么可以让UIWebView为您处理重定向。在这个站点中,我通过将用户代理设置为mobilesafari使其正常工作。我还删除了
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)
  }

如果我没有代码假装是mobilesafari,那么页面内容会加载,但不会做正确的事情。它只是停在一个旋转器上。我假设网站发送的Javascript不同,这取决于它认为与哪个渲染引擎对话。如果您没有指定任何用户代理,那么在指定默认行为方面,无论他们的web设计师做了什么,您都将任由其摆布。

我对服务器没有影响。正如我所说,它在mobile Safari中运行良好。你怎么解释呢?Javascript将页面设置为不可见(display=none),然后重定向到一个新页面(可能是一个非常适合移动设备的页面)。这可能在Safari中发生得太快了,以至于你根本没有注意到。是的,但我如何在UIWebView中实现这一点?请你在回答中解释一下你是怎么做的。我不明白你为什么需要注册一个用户代理?如果他回答了你最初的问题,你应该奖励他。相反,您会不断向原始需求添加更多内容。这就是所谓的“移动球门柱”,这是一种对待他人的糟糕方式。也有同样的问题。在
viewdide
中调用
loadRequest
对我来说很有效!