我使用UIWebView
并想加载以下网址:
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
在移动版Safari和UIWebView中加载上述网址时,会得到不同的结果。不是在呈现页面方面,而是向用户显示的最终页面是不同的。
以下是我在iOS7中使用的代码:
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()
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])
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加载移动Safari之类的网址?
编辑:以下是我使用以下网址时的外观:
http://feratel.lueneburger-heide.de/lhg/de/accommodation/detail/LUH/8af6d1fd-af6d-4765-8025-9eb8fa05ea42/hotel%20undeloher%20hof?customHeader=true
UIWebView中的:
以下是它在移动Safari中的外观: