con*_*ile 16 https uiwebview swift
注意:这个问题仍然没有答案!
我使用a 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
Run Code Online (Sandbox Code Playgroud)
请注意,上面的网址不是我的网址,因此我无法更改其内容.
尝试加载时,我收到以下错误func webView(webView: UIWebView, didFailLoadWithError error: NSError)
:
错误域= NSURLErrorDomain代码= -999"操作无法完成.(NSURLErrorDomain错误-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
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里用上面的代码创建了一个示例项目:https://github.com/confile/UIWebView-https-url
如何UIWebView
正确获取我的负载?
Ewa*_*lor 12
我运行了你的代码.它大约50%的时间工作正常,并且其他时间显示-999错误.
NSURLErrorCancelled
根据Apple的Foundation Constants Reference中的URL加载系统错误代码,该错误代码.所以有些东西正在取消页面加载.
我打印了收到的页面内容,使用:
println(aWebView.stringByEvaluatingJavaScriptFromString("document.body.innerHTML"));
Run Code Online (Sandbox Code Playgroud)
我看到页面中的Javascript包含这个:
if (mobilePath) {
document.body.style.display = 'none';
document.location = mobilePath;
}
Run Code Online (Sandbox Code Playgroud)
我的猜测是,一旦内容加载,这个Javascript就会开始运行.这会导致竞争条件,有时页面会立即重定向到不同document.location
的页面,因此在完全加载之前会取消第一页加载.
您应该更改服务器,以便它不发送此Javascript.
或者,如果您只是想像普通网站一样加载网站,那么您可以让UIWebView为您处理重定向.对于这个站点,我通过将User-agent设置为伪装成移动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)
}
Run Code Online (Sandbox Code Playgroud)
如果我没有假装成移动Safari的代码,那么页面内容会加载,但它不会做正确的事情.它只是停在旋转器上.我认为该网站正在发送不同的Javascript,具体取决于它认为正在与之交谈的渲染引擎.如果您没有指定任何用户代理,那么您将无视其Web设计人员在指定默认行为方面所做的任何事情.
归档时间: |
|
查看次数: |
9327 次 |
最近记录: |