What is a webView exactly?

I'm setting up a new business app that I want to get some infomation and a page from a website. I don't know how to get a webView to work and I can't find any tutorials that show how to use one. Can someone please show me how to activate a webView?

1 answer

  • answered 2019-08-13 05:06 Gowri K

    WebView is an object that displays interactive web content, such as for an in-app browser. Implementation steps:

    1. First and foremost, In the Storyboard, add WebKit View.
    2. In the Viewcontroller.swift file, import WebKit.
    3. create an @IBOutlet for the webview. (eg: @IBOutlet weak var webView: WKWebView!) or create webview programmatically.
    4. Change the class declaration to implement the WKNavigationDelegate protocol. (eg: class ViewController: UIViewController, WKNavigationDelegate {})
    5. In the viewDidLoad function, Add : webView.scrollView.bounces = false webView.navigationDelegate = self let url = URL(string: "https:www.example.com")! webView.load(URLRequest(url: url))
    6. following are the useful webview delegates: func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {} func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {}

    Full code:

    import UIKit
    import WebKit
    class ViewController: UIViewController, WKNavigationDelegate {
    var webView: WKWebView!
    override func viewDidLoad() {
      super.viewDidLoad()
      let url = URL(string: "https://www.example.com")!
      webView.load(URLRequest(url: url))
      let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
      toolbarItems = [refresh]
      navigationController?.isToolbarHidden = false
      }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
      title = webView.title 
      }
    }