Web View
WebView is an object that can show interactive web content and load HTML strings for an in-app browser within an iOS application. It's a WKWebView instance, which is derived from the UIView class.
class WKWebView : UIView
As previously stated, the WebView object may be used to load web information into an iOS application. To load web material, we just need to build a WKWebView object, set it as a view, and send it a request.
WKWebView Properties and Methods
The WKWebView class has attributes and methods that may be used to modify the webview's functionality. We can utilize the goBack() and goForward() methods to allow users to go backward and forward. We can utilize the Boolean attributes canGoBack and canGoForward to see if the user can travel in a specific direction.
Example: Creating WKWebView programmatically
import UIKit
import WebKit
class ViewController: UIViewController {
var webView : WKWebView!
let activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
override func viewDidLoad() {
super.viewDidLoad()
After loading the view, do any extra configuration.
activityIndicator.startAnimating()
webView = WKWebView()
webView.navigationDelegate = self
self.view = webView
let loadURL = "https://www.javatpoint.com"
let url = URL(string: loadURL)!
webView.load(URLRequest(url: url))
activityIndicator.stopAnimating()
webView.allowsBackForwardNavigationGestures = true
}
}
extension ViewController : WKNavigationDelegate {
func webView(_ webView: WKWebView, didFail navigation:
WKNavigation!, withError error: Error) {
activityIndicator.startAnimating()
}
didFinish navigation: WKNavigation!) func webView(_ webView: WKWebView,
didFinish navigation: WKNavigation!) {
activityIndicator.stopAnimating()
}
}