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 initWithFrame:configuration: method, you need to load the web content. Use the loadHTMLString:baseURL: method to begin loading local HTML files or the loadRequest: method to begin loading web content. Use the stopLoading method to stop loading, and the loading 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 WKDataDetectorTypePhoneNumber flag.

You can also use the setMagnification:centeredAtPoint: 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

handlesURLScheme:

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

Beta

Initializing a Web View

configuration

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

initWithFrame:configuration:

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

Inspecting the View Information

scrollView

The scroll view associated with the web view.

title

The page title.

URL

The active URL.

customUserAgent

The custom user agent string.

serverTrust

A SecTrustRef object for the currently committed navigation.

certificateChain

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

Deprecated

Setting Delegates

navigationDelegate

The web view's navigation delegate.

UIDelegate

The web view's user interface delegate.

Loading Content

estimatedProgress

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

hasOnlySecureContent

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

loadHTMLString:baseURL:

Sets the webpage contents and base URL.

loading

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

reload

Reloads the current page.

reload:

Reloads the current page.

reloadFromOrigin

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

reloadFromOrigin:

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

stopLoading

Stops loading all resources on the current page.

stopLoading:

Stops loading all resources on the current page.

loadData:MIMEType:characterEncodingName:baseURL:

Sets the webpage contents and base URL.

loadFileURL:allowingReadAccessToURL:

Navigates to the requested file URL on the filesystem

Scaling Content

allowsMagnification

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

magnification

The factor by which the page content is currently scaled.

setMagnification:centeredAtPoint:

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

Navigating

allowsBackForwardNavigationGestures

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

backForwardList

The web view's back-forward list.

canGoBack

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

canGoForward

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

allowsLinkPreview

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

goBack

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

goBack:

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

goForward

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

goForward:

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

goToBackForwardListItem:

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

loadRequest:

Navigates to a requested URL.

Executing JavaScript

evaluateJavaScript:completionHandler:

Evaluates a JavaScript string.

Taking Snapshots

takeSnapshotWithConfiguration:completionHandler:

Takes a snapshot of the view's visible viewport.

Beta

Relationships

Inherits From

See Also

Initialization

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.

WKProcessPool

A WKProcessPool object represents a pool of Web Content processes.

WKWindowFeatures

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

WKWebViewConfiguration

A collection of properties used to initialize a web view.

WKPreferences

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

WKUIDelegate

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

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software