注意:这个问题还没有答案!
我使用 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
请注意,以上网址不是我的网址,因此我无法更改其内容。
尝试加载时,我从 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 错误。
该错误代码是 NSURLErrorCancelled
,根据 URL Loading System Error Codes in Apple's Foundation Constants Reference .所以有些东西正在取消页面加载。
我使用这个打印了收到的页面内容:
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,具体取决于它认为正在与之通信的渲染引擎。如果您不指定任何用户代理,那么您将受制于他们的网页设计师在指定默认行为方面所做的一切。
关于swift - UIWebView 不加载 HTTPS 页面 : Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’ t be completed.(NSURLErrorDomain 错误 -999。)”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32121767/