WKWebView
An object that displays interactive web content, such as for an in-app browser.
Declaration
@MainActor class WKWebViewMentioned in
Overview
A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app’s UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app’s native views. Use it when web technologies satisfy your app’s layout and styling requirements more readily than native views. For example, you might use it when your app’s content changes frequently.
A web view offers control over the navigation and user experience through delegate objects. Use the navigation delegate to react when the user clicks links in your web content, or interacts with the content in a way that affects navigation. For example, you might prevent the user from navigating to new content unless specific conditions are met. Use the UI delegate to present native UI elements, such as alerts or contextual menus, in response to interactions with your web content.
Embed a WKWebView object programmatically into your view hierarchy, or add it using Interface Builder. Interface Builder supports many customizations, such as configuring data detectors, media playback, and interaction behaviors. For more extensive customizations, create your web view programmatically using a WKWebViewConfiguration object. For example, use a web view configuration object to specify handlers for custom URL schemes, manage cookies, and customize preferences for your web content.
Before your web view appears onscreen, load content from a web server using a URLRequest structure or load content directly from a local file or HTML string. The web view automatically loads embedded resources such as images or videos as part of the initial load request. It then renders your content and displays the results inside the view’s bounds rectangle. The following code example shows a view controller that replaces its default view with a custom WKWebView object.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}A web view automatically converts telephone numbers that appear in web content to Phone links. When the user taps a Phone link, the Phone app launches and dials the number. Use the WKWebViewConfiguration object to change the default data detector behavior.
You can also use setMagnification(_:centeredAt:) to programmatically set the scale of web content the first time it appears in a web view. Thereafter, the user can change the scale using gestures.
Manage the navigation through your web content
WKWebView provides a complete browsing experience, including the ability to navigate between different webpages using links, forward and back buttons, and more. When the user clicks a link in your content, the web view acts like a browser and displays the content at that link. To disallow navigation, or to customize your web view’s navigation behavior, provide your web view with a navigation delegate — an object that conforms to the WKNavigationDelegate protocol. Use your navigation delegate to modify the web view’s navigation behavior, or to track the loading progress of new content.
You can also use the methods of WKWebView to navigate programmatically through your content, or to trigger navigation from other parts of your app’s interface. For example, if your UI includes forward and back buttons, connect those buttons to the goBack(_:) and goForward(_:) methods of your web view to trigger the corresponding web navigation. Use the canGoBack and canGoForward properties to determine when to enable or disable your buttons.
Provide sharing options
People may want to share the contents of your web view with an app or other people. Use a UIActivityViewController to present a share sheet offering all the ways people can share the web content.
If your app has the com.apple.developer.web-browser entitlement, the iOS share sheet can offer Add to Home Screen for an http or https webpage, creating a convenient link to a web app or bookmark. To allow someone to add the current webpage to the Home Screen, include the WKWebView instance in the activityItems array when you call init(activityItems:applicationActivities:) to create the UIActivityViewController. For more information about building a browser app, see Preparing your app to be the default web browser.
Topics
Creating a web view
Determining whether WebKit can load content
Displaying native user interface elements
Managing navigation between webpages
Loading web content
load(_:)load(_:mimeType:characterEncodingName:baseURL:)loadHTMLString(_:baseURL:)loadFileRequest(_:allowingReadAccessTo:)loadFileURL(_:allowingReadAccessTo:)loadSimulatedRequest(_:response:responseData:)loadSimulatedRequest(_:responseHTML:)isLoadingestimatedProgress
Managing the loading process
Managing downloads
Making web content inspectable
Inspecting the view information
scrollViewtitleurlmediaTypecustomUserAgentserverTrusthasOnlySecureContentthemeColorunderPageBackgroundColor
Scaling content
Interacting with media
pauseAllMediaPlayback(completionHandler:)requestMediaPlaybackState(completionHandler:)setAllMediaPlaybackSuspended(_:completionHandler:)closeAllMediaPresentations(completionHandler:)WKMediaPlaybackState
Managing the microphone and camera
cameraCaptureStatemicrophoneCaptureStatesetCameraCaptureState(_:completionHandler:)setMicrophoneCaptureState(_:completionHandler:)WKMediaCaptureState
Searching the current page’s content
Navigating between webpages
allowsBackForwardNavigationGesturesbackForwardListgoBack(_:)goBack()goForward(_:)goForward()go(to:)canGoBackcanGoForwardallowsLinkPreviewinteractionState
Executing JavaScript
evaluateJavaScript(_:completionHandler:)evaluateJavaScript(_:in:in:completionHandler:)evaluateJavaScript(_:in:contentWorld:)callAsyncJavaScript(_:arguments:in:in:completionHandler:)callAsyncJavaScript(_:arguments:in:contentWorld:)
Capturing the web view’s content
takeSnapshot(with:completionHandler:)createPDF(configuration:completionHandler:)pdf(configuration:)createWebArchiveData(completionHandler:)printOperation(with:)WKSnapshotConfigurationWKPDFConfiguration