How to Migrate to WKWebView?

后端 未结 9 1406
后悔当初
后悔当初 2020-11-30 22:23

I\'m trying to understand how to make use of the new WKWebView in iOS8, can\'t find much information. I\'ve read:

http://developer.telerik.com/featured/why-ios-8s-wk

9条回答
  •  一生所求
    2020-11-30 23:09

    WkWebView is much faster and reliable than UIWebview according to the Apple docs. Here, I posted my WkWebViewController.

    import UIKit
    import WebKit
    
    class WebPageViewController: UIViewController,UINavigationControllerDelegate,UINavigationBarDelegate,WKNavigationDelegate{
    
        var webView: WKWebView?
        var webUrl="http://www.nike.com"
    
        override func viewWillAppear(animated: Bool){
            super.viewWillAppear(true)
            navigationController!.navigationBar.hidden = false
    
        }
        override func viewDidLoad()
        {
            /* Create our preferences on how the web page should be loaded */
            let preferences = WKPreferences()
            preferences.javaScriptEnabled = false
    
            /* Create a configuration for our preferences */
            let configuration = WKWebViewConfiguration()
            configuration.preferences = preferences
    
            /* Now instantiate the web view */
            webView = WKWebView(frame: view.bounds, configuration: configuration)
    
            if let theWebView = webView{
                /* Load a web page into our web view */
                let url = NSURL(string: self.webUrl)
                let urlRequest = NSURLRequest(URL: url!)
                theWebView.loadRequest(urlRequest)
                theWebView.navigationDelegate = self
                view.addSubview(theWebView)
    
            }
    
    
        }
        /* Start the network activity indicator when the web view is loading */
        func webView(webView: WKWebView,didStartProvisionalNavigation navigation: WKNavigation){
                UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        }
    
        /* Stop the network activity indicator when the loading finishes */
        func webView(webView: WKWebView,didFinishNavigation navigation: WKNavigation){
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        }
    
        func webView(webView: WKWebView,
            decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse,decisionHandler: ((WKNavigationResponsePolicy) -> Void)){
                //print(navigationResponse.response.MIMEType)
                decisionHandler(.Allow)
    
        }
        override func didReceiveMemoryWarning(){
            super.didReceiveMemoryWarning()
        }
    
    }
    

提交回复
热议问题