WKWebView object displays interactive web content, such as for an in-app browser. You can use the
WKWebView class to embed web content in your app. To do so, create a WKWebView object, set it as the view, and send it a request to load web content.
- iOS 8.0+
- macOS 10.10+
After creating a new WKWebView object using the
init(frame:configuration:) method, you need to load the web content. Use the
loadHTMLString(_:baseURL:) method to begin loading local HTML files or the
load(_:) method to begin loading web content. Use the
stopLoading() method to stop loading, and the
isLoading property to find out if a web view is in the process of loading. Set the delegate property to an object conforming to the
WKUIDelegate protocol to track the loading of web content. See Listing 1 for an example of creating a
To allow the user to move back and forward through the webpage history, use the
goForward() methods as actions for buttons. Use the
canGoForward properties to disable the buttons when the user can’t move in a direction.
By default, a web view automatically converts telephone numbers that appear in web content to Phone links. When a Phone link is tapped, the Phone app launches and dials the number. To turn off this default behavior, set the
dataDetectorTypes property with a
WKDataDetectorTypes bitfield that does not contain the
You can also use the
setMagnification(_:centeredAt:) to programmatically set the scale of web content the first time it is displayed in a web view. Thereafter, the user can change the scale using gestures.