Class

WKWebView

A 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.

Overview

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 WKWebView programmatically.

Listing 1

Creating a WKWebView programmatically

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)
    }}

To allow the user to move back and forward through the webpage history, use the goBack() and goForward() methods as actions for buttons. Use the canGoBack and 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 phoneNumber flag.

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.

Topics

Determining Whether WebKit can Load Content

class func handlesURLScheme(String)

Returns whether WebKit natively supports loading resources with a particular URL scheme.

Initializing a Web View

var configuration: WKWebViewConfiguration

A copy of the configuration to initialize the web view with.

init(frame: CGRect, configuration: WKWebViewConfiguration)

Returns a web view initialized with a specified frame and configuration.

Inspecting the View Information

var scrollView: UIScrollView

The scroll view associated with the web view.

var title: String?

The page title.

var url: URL?

The active URL.

var customUserAgent: String?

The custom user agent string.

var serverTrust: SecTrust?

A SecTrustRef object for the currently committed navigation.

var certificateChain: [Any]

An array of objects forming the certificate chain for the currently committed navigation.

Deprecated

Setting Delegates

var navigationDelegate: WKNavigationDelegate?

The web view's navigation delegate.

var uiDelegate: WKUIDelegate?

The web view's user interface delegate.

Loading Content

var estimatedProgress: Double

An estimate of what fraction of the current navigation has been loaded.

var hasOnlySecureContent: Bool

A Boolean value indicating whether all resources on the page have been loaded through securely encrypted connections.

func loadHTMLString(String, baseURL: URL?)

Sets the webpage contents and base URL.

var isLoading: Bool

A Boolean value indicating whether the view is currently loading content.

func reload()

Reloads the current page.

func reload(Any?)

Reloads the current page.

func reloadFromOrigin()

Reloads the current page, performing end-to-end revalidation using cache-validating conditionals if possible.

func reloadFromOrigin(Any?)

Reloads the current page, performing end-to-end revalidation using cache-validating conditionals if possible.

func stopLoading()

Stops loading all resources on the current page.

func stopLoading(Any?)

Stops loading all resources on the current page.

func loadFileURL(URL, allowingReadAccessTo: URL)

Navigates to the requested file URL on the filesystem

Scaling Content

var allowsMagnification: Bool

A Boolean value indicating whether magnify gestures will change the web view’s magnification.

var magnification: CGFloat

The factor by which the page content is currently scaled.

func setMagnification(CGFloat, centeredAt: CGPoint)

Scales the page content by a specified factor and centers the result on a specified point.

Navigating

var allowsBackForwardNavigationGestures: Bool

A Boolean value indicating whether horizontal swipe gestures will trigger back-forward list navigations.

var backForwardList: WKBackForwardList

The web view's back-forward list.

var canGoBack: Bool

A Boolean value indicating whether there is a back item in the back-forward list that can be navigated to.

var canGoForward: Bool

A Boolean value indicating whether there is a forward item in the back-forward list that can be navigated to.

var allowsLinkPreview: Bool

A Boolean value that determines whether pressing on a link displays a preview of the destination for the link.

func goBack()

Navigates to the back item in the back-forward list.

func goBack(Any?)

Navigates to the back item in the back-forward list.

func goForward()

Navigates to the forward item in the back-forward list.

func goForward(Any?)

Navigates to the forward item in the back-forward list.

func go(to: WKBackForwardListItem)

Navigates to an item from the back-forward list and sets it as the current item.

func load(URLRequest)

Navigates to a requested URL.

Taking Snapshots

See Also

Initialization

protocol WKNavigationDelegate

The methods of the WKNavigationDelegate protocol help you implement custom behaviors that are triggered during a web view's process of accepting, loading, and completing a navigation request.

class WKProcessPool

A WKProcessPool object represents a pool of Web Content processes.

class WKWindowFeatures

A WKWindowFeatures object specifies optional attributes for the containing window when a new web view is requested.

class WKWebViewConfiguration

A collection of properties used to initialize a web view.

class WKPreferences

A WKPreferences object encapsulates the preference settings for a web view.

protocol WKUIDelegate

The WKUIDelegate class provides methods for presenting native user interface elements on behalf of a webpage.