2015-09-02 34 views
3

我使用UIWebView並希望加載以下網址:UIWebView確實加載了與移動Safari不同的URL?

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加載的URL,如移動Safari瀏覽器呢?

編輯:這裏是它的外觀,當我使用以下網址:

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

enter image description here

,這裏是它的外觀在移動Safari瀏覽器:

enter image description here

+0

我試圖加載的URL,但沒有給我看了,我逃出後網址中的一些字符串,它的加載方式與移動Safari一樣 –

+0

@ T_77如果它適合您,請您提供一個答案。 – confile

+0

如果你張貼它在你身邊表現的截圖,那就太好了。 –

回答

0

enter image description here enter image description here這爲我工作:

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://feratel.lueneburger-heide.de/lhg/de/accommodation/detail/LUH/8634e147-e13d-40f5-8954-2ac40cfea2a7/romantik_hotel_bergstrom?customHeader=true"]]]; 

我需要替換URL中的「O」與「O」字

+0

我沒有照顧的限制,但他們都顯示相同的內容 –

+0

,你也需要逃避的一部分字符串 –

+0

第一個是Safari瀏覽器,第二個圖像是webview –