iOS: Web View
WebView is an object that can load HTML strings for an in-app browser within an iOS application and display interactive web content. It is a WKWebView instance, which is derived from the UIView class.
class WKWebView : UIView
As previously noted, the WebView object may be used to load web content into an iOS application. To load web material, we only need to create an instance of a WKWebView object, set it as a view, and send it a request.
WKWebView Properties and Methods
To alter the webview’s behavior, utilize the attributes and methods found in the WKWebView class. We may utilize the goBack() and goForward() methods to allow the user to travel both forward and backward. We may utilize the Boolean values canGoBack and canGoForward to see if the user can travel in a specific direction.
Additionally, WebView turns the phone number into links that, when clicked, take the user to a dialer pad that already has the tapped number entered.
WKWebView Properties
SN | Property | Description |
---|---|---|
1 | var scrollView: UIScrollView | It represents the scrollview associated with the web view. |
2 | var title: String? | It is a string object which represents the page title. |
3 | var url: URL? | It is an instance of the URL class, which represents the active URL. |
4 | var customUserAgent: String? | It is a string object which represents custom user agent string. |
5 | var serverTrust: SecTrust? | A SecTrustRef object for the currently committed navigation. |
6 | var navigationDelegate: WKNavigationDelegate? | It is the web view’s navigation delegate. |
7 | var uiDelegate: WKUIDelegate? | It represents the web view’s UI delegate. |
8 | var estimatedProgress: Double | It is a double type object which represents the estimate of what fraction of the current navigation has been loaded. |
9 | var hasOnlySecureContent: Bool | It is a Boolean type value that determines whether all resources on the page have been loaded through securely encrypted connections. |
10 | var isLoading: Bool | It is a Boolean type value that indicates whether the view is currently loading the web content. |
11 | var allowsMagnification: Bool | It is a Boolean type value that indicates whether magnify gestures will change the web view’s magnification. |
12 | var magnification: CGFloat | It is the factor by which the web page content is currently scaled. |
13 | var allowsBackForwardNavigationGestures: Bool | A Boolean value indicating whether horizontal swipe gestures will trigger back-forward list navigations. |
14 | var backForwardList: WKBackForwardList | It is the web view’s back forward list. |
15 | var canGoBack: Bool | It is a Boolean type value that indicates whether there is a back item that exists in the back-forward list. |
16 | var canGoForward: Bool | It is a Boolean type value that indicates whether there is a forward item that exists in the back-forward list. |
17 | var allowsLinkPreview: Bool | It is a Boolean type value that controls whether tapping on a link would display the preview of the link destination. |
WKWebView Methods
SN | Method | Description |
---|---|---|
1 | func loadHTMLString(String, baseURL: URL?) -> WKNavigation? | This method is used to set the webpage contents and base URL. |
2 | func reload() -> WKNavigation? | This method reloads the current page. |
3 | func reloadFromOrigin() -> WKNavigation? | This method reloads the current page, performing end-to-end revalidation using cache-validating conditionals if possible. |
4 | func stopLoading(Any?) | This method stops loading all the resources of the current page. |
5 | func load(Data, mimeType: String, characterEncodingName: String, baseURL: URL) -> WKNavigation? | This method is used to set the webpage contnts and base url. |
6 | func loadFileURL(URL, allowingReadAccessTo: URL) -> WKNavigation? | This method navigates to the file URL requested on the file system. |
7 | func setMagnification(CGFloat, centeredAt: CGPoint) | This method is used to scale the page content by a specified factor and centers the result on a specified point. |
8 | func goBack() -> WKNavigation? | This method navigates to the back-item present in the back-forward list. |
10 | func goForward() -> WKNavigation? | This method navigates to the forward-item present in the back-forward list. |
12 | func go(to: WKBackForwardListItem) -> WKNavigation? | This method navigates to the specified item from the back-forward list. |
13 | func load(URLRequest) -> WKNavigation? | This method loads the content of the specified URL request. |
14 | func evaluateJavaScript(String, completionHandler: ((Any?, Error?) -> Void)?) | This method is used to evaluate the javascript string. |
15 | func takeSnapshot(with: WKSnapshotConfiguration?, completionHandler: (UIImage?, Error?) -> Void) | This method takes a snapshot of the view’s visible viewport. |