2015-09-07 111 views
15

我有應裝入以下網址WKWebView:WKWebView不會加載https URL?

https://buchung.salonmeister.de/place/#offer-details-page?id=907599&venueId=301655 

她是我使用的代碼:

import UIKit 
import WebKit 


class MMWKBrowserController: UIViewController { 

    private let closeButtonSelector: Selector = "closeButtonTapped:" 

    private var urlString: String 
    private let request: NSMutableURLRequest 

    private var webView: WKWebView! 
    private var twoLineTitleView: UIView! 
    private var titleLabel: UILabel? 
    private var subTitleLabel: UILabel? 
    private var indicator: UIActivityIndicatorView! 


    init(urlString: String) { 
    self.urlString = urlString 

    println("*** Using MMWKBrowserController ***") 

    var url: NSURL? = NSURL(string: urlString) 
    if url == nil { 
     var escapedString: String = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! 
     self.urlString = escapedString 
     url = NSURL(string: escapedString) 
    } 

    println("url: \(url)") 
    request = NSMutableURLRequest(URL: url!) 

    request.setValue("Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H141 Safari/600.1.4", forHTTPHeaderField: "UserAgent") 

    super.init(nibName: nil, bundle: nil) 
    } 

    required init(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
    } 


    deinit { 
    self.webView.removeObserver(self, forKeyPath: "loading") 
    self.webView.removeObserver(self, forKeyPath: "title") 
    self.webView.removeObserver(self, forKeyPath: "URL") 
    self.webView.removeObserver(self, forKeyPath: "estimatedProgress") 
    self.webView.stopLoading() 
    } 


    override func viewDidLoad() { 
    super.viewDidLoad() 
    createNavigationView() 


    self.navigationController?.navigationBar.tintColor = MGColor.actionColor 

    let config = WKWebViewConfiguration() 
    self.webView = WKWebView(frame: self.view.bounds, configuration: config) 
    self.view.addSubview(self.webView) 


    indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray) 
    //indicator.backgroundColor = UIColor(white: 0.1, alpha: 0.5) 
    webView.addSubview(indicator) 

    self.webView.snp_makeConstraints { (make) -> Void in 
     make.edges.equalTo(self.view) 
    } 

    indicator.snp_makeConstraints { (make) -> Void in 
     make.center.equalTo(self.webView) 
    } 

    webView.addObserver(self, forKeyPath: "loading", options: NSKeyValueObservingOptions.New, context: nil) 
    webView.addObserver(self, forKeyPath: "title", options: NSKeyValueObservingOptions.New, context: nil) 
    webView.addObserver(self, forKeyPath: "URL", options: NSKeyValueObservingOptions.New, context: nil) 
    webView.addObserver(self, forKeyPath: "estimatedProgress", options: NSKeyValueObservingOptions.New, context: nil) 
    } 


    override func viewDidDisappear(animated: Bool) { 
    super.viewDidDisappear(animated) 
    self.webView.stopLoading() 
    } 


    private func createNavigationView() { 
    let closeItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop, target: self, action: closeButtonSelector) 
    self.navigationItem.leftBarButtonItem = closeItem 

    // create center view 
    let titleViewWidth = self.view.frame.size.width - 100 

    twoLineTitleView = UIView(frame: CGRectMake(0, 0, titleViewWidth, 44)) 

    titleLabel = UILabel(frame: CGRectMake(0, 6, titleViewWidth, 16)) 
    titleLabel?.backgroundColor = UIColor.clearColor() 
    titleLabel?.font = UIFont.boldSystemFontOfSize(16) 
    titleLabel?.textAlignment = NSTextAlignment.Center 

    subTitleLabel = UILabel(frame: CGRectMake(0, 21, titleViewWidth, 20)) 
    subTitleLabel?.backgroundColor = UIColor.clearColor() 
    subTitleLabel?.font = UIFont.systemFontOfSize(10) 
    subTitleLabel?.textAlignment = NSTextAlignment.Center 

    twoLineTitleView.addSubview(titleLabel!) 
    twoLineTitleView.addSubview(subTitleLabel!) 
    self.navigationItem.titleView = twoLineTitleView 
    } 



    override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    self.webView.loadRequest(self.request) 

    } 


    func closeButtonTapped(sender: UIBarButtonItem) { 
    self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil) 
    } 


    override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) { 

    if let wk = object as? WKWebView { 
     switch keyPath { 
     case "loading": 
     if let val: AnyObject = change[NSKeyValueChangeNewKey] { 
      if let val = val as? Bool { 
      if val { 
       self.indicator.startAnimating() 
      } 
      else { 
       self.indicator.stopAnimating() 
      } 
      } 
     } 
     case "title": 
     self.titleLabel?.text = self.webView.title 
     case "URL": 
     self.subTitleLabel?.text = self.webView.URL?.URLString 
     case "estimatedProgress": 
     println("progress: \(Int32(self.webView.estimatedProgress*100))") 

     default: break 
     } 
    } 
    } 


} 

注:我使用SDK的iOS 8.4

爲什麼移動Safari瀏覽器加載此網址,但WKWebView不?

+0

您正在使用哪個SDK? iOS8或iOS 9? – itsji10dra

+0

我使用SDK iOS 8.4 – confile

+0

這部分url加載正常:http://buchung.salonmeister.de,其餘部分可能有一些字符不能被webview套件重新識別。嘗試不同的選擇。 –

回答

1

不知道,如果同樣的錯誤原因,但問題是一樣的,我iOS9

一些領域無法加載

下橫空出世,這個問題是

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler { 

,並提供後臺

completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); 

哪裏我應該我是用錯誤的代碼從https://github.com/ShingoFukuyama/WKWebViewTips

+0

我沒有明白你的觀點。你能否在描述中更詳細些?我不使用方法''didReceiveAuthenticationChallenge''。 – confile

+0

我確實遇到類似的錯誤行爲,似乎webview沒有加載https,原來這是一個問題在didReceiveAuthenticationChallenge –

+0

您可以請發佈更多的代碼。你的''didReceiveAuthenticationChallenge''方法是怎樣的? – confile

19

添加到您的plist

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
</dict> 

下面是9.0 http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

此外,如果這一變化的解釋返回

completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); 

你想更安全地設置它,它會給你一個更復雜的方法來做到這一點。

+3

耶穌,停止發佈這個。有用 ...直到App Store拒絕你的應用。 – Josh

0

我有一個網站也有一個類似的問題,它也受到高安全性TLS 1.2證書的保護。爲了讓WKWebView接受服務器的證書,我將這段代碼添加到了我的Web視圖控制器代理中:

-(void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler 
{ 
    if ([[[challenge protectionSpace]authenticationMethod] isEqualToString: @"NSURLAuthenticationMethodServerTrust"]) { 
     SecTrustRef serverTrust = challenge.protectionSpace.serverTrust; 
     CFDataRef exceptions = SecTrustCopyExceptions(serverTrust); 
     SecTrustSetExceptions(serverTrust, exceptions); 
     CFRelease(exceptions); 
     newCredential = [NSURLCredential credentialForTrust:serverTrust]; 
     completionHandler(NSURLSessionAuthChallengeUseCredential, newCredential); 
    } else { 
     completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, newCredential); 
    } 
}