Search code examples
iosswiftuiwebviewwkwebview

Migrate from UIWebview to WKWebview


I need to migrating from UIWebView to WKWebView but i cant. Does anyone know?

importUIKit

class ViewController: UIViewController {

let URL:String = "https://google.com"

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    let url = NSURL (string: URL);
    let requestObj = NSURLRequest(url: url! as URL);
    webView.loadRequest(requestObj as URLRequest);
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

Solution

  • You only need to change, the UIWebView for WKWebView add import WebKit and change the class name for your Outlet to WKWebView, also change the loadRequest for load

    import UIKit
    import WebKit
    
    
    
    class ViewController: UIViewController {
    
     @IBOutlet weak var webView: WKWebView!
    
          let urlString: String = "https://google.com"
    
         override func viewDidLoad() {
             super.viewDidLoad()
             guard let url =  URL(string: urlString) else { return }
             let requestObj = URLRequest(url: url)
             webView.load(requestObj)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    }