iOS Developer Library

Developer

WebKit Framework Reference WKWebView Class Reference

Options
Deployment Target:

On This Page
Language:

WKWebView

A WKWebView object displays interactive web content, such as for an in-app browser.

For new development, employ this class instead of the older UIWebView class.

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

    Declaration

    Swift

    init(frame frame: CGRect, configuration configuration: WKWebViewConfiguration)

    Objective-C

    - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration

    Parameters

    frame

    The frame for the new web view.

    configuration

    The configuration for the new web view.

    Return Value

    An initialized web view, or nil if the object could not be initialized.

    Discussion

    This is a designated initializer that accepts a custom WKWebViewConfiguration. The initializer copies the specified configuration. Changing the configuration object after initializing the web view has no effect on the web view. You can use the initWithFrame: method to initialize an instance with the default configuration. See WKWebViewConfiguration for default values.

    Availability

    Available in iOS 8.0 and later.

  • A copy of the configuration to initialize the web view with. (read-only)

    Declaration

    Swift

    @NSCopying var configuration: WKWebViewConfiguration { get }

    Objective-C

    @property(nonatomic, readonly, copy) WKWebViewConfiguration *configuration

    Availability

    Available in iOS 8.0 and later.

  • The scroll view associated with the web view. (read-only)

    Declaration

    Swift

    var scrollView: UIScrollView { get }

    Objective-C

    @property(nonatomic, readonly, strong) UIScrollView *scrollView

    Availability

    Available in iOS 8.0 and later.

  • The page title. (read-only)

    Declaration

    Swift

    var title: String? { get }

    Objective-C

    @property(nonatomic, readonly, copy) NSString *title

    Discussion

    The WKWebView class is key-value observing (KVO) compliant for this property.

    Availability

    Available in iOS 8.0 and later.

  • URL URL Property

    The active URL. (read-only)

    Declaration

    Swift

    @NSCopying var URL: NSURL? { get }

    Objective-C

    @property(nonatomic, readonly, copy) NSURL *URL

    Discussion

    This is the URL that should be reflected in the user interface.

    The WKWebView class is key-value observing (KVO) compliant for this property.

    Availability

    Available in iOS 8.0 and later.

  • The custom user agent string or nil if no custom user agent string has been set.

    Declaration

    Swift

    var customUserAgent: String?

    Objective-C

    @property(nonatomic, copy) NSString *customUserAgent

    Availability

    Available in iOS 9.0 and later.

  • An array of objects forming the certificate chain for the currently committed navigation. (read-only)

    Declaration

    Swift

    var certificateChain: [AnyObject] { get }

    Objective-C

    @property(nonatomic, readonly, copy) NSArray *certificateChain

    Discussion

    Each item in the array is a SecCertificateRef object.

    Availability

    Available in iOS 9.0 and later.

  • An estimate of what fraction of the current navigation has been loaded. (read-only)

    Declaration

    Swift

    var estimatedProgress: Double { get }

    Objective-C

    @property(nonatomic, readonly) double estimatedProgress

    Discussion

    This value ranges from 0.0 to 1.0 based on the total number of bytes expected to be received, including the main document and all of its potential subresources. After a navigation loading completes, the estimatedProgress remains at 1.0 until a new navigation starts, at which point the estimatedProgress is reset to 0.0.

    The WKWebView class is key-value observing (KVO) compliant for this property.

    Availability

    Available in iOS 8.0 and later.

  • A Boolean value indicating whether all resources on the page have been loaded through securely encrypted connections. (read-only)

    Declaration

    Swift

    var hasOnlySecureContent: Bool { get }

    Objective-C

    @property(nonatomic, readonly) BOOL hasOnlySecureContent

    Discussion

    The WKWebView class is key-value observing (KVO) compliant for this property.

    Availability

    Available in iOS 8.0 and later.

  • Sets the webpage contents and base URL.

    Declaration

    Swift

    func loadHTMLString(_ string: String, baseURL baseURL: NSURL?) -> WKNavigation?

    Objective-C

    - (WKNavigation *)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

    Parameters

    string

    The string to use as the contents of the webpage.

    baseURL

    A URL used to resolve relative URLs within the document.

    Return Value

    A new navigation.

    Availability

    Available in iOS 8.0 and later.

  • A Boolean value indicating whether the view is currently loading content. (read-only)

    Declaration

    Swift

    var loading: Bool { get }

    Objective-C

    @property(nonatomic, readonly, getter=isLoading) BOOL loading

    Discussion

    The WKWebView class is key-value observing (KVO) compliant for this property.

    Availability

    Available in iOS 8.0 and later.

  • Reloads the current page.

    Declaration

    Swift

    func reload() -> WKNavigation?

    Objective-C

    - (WKNavigation *)reload

    Return Value

    A new navigation representing the reload.

    Availability

    Available in iOS 8.0 and later.

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

    Declaration

    Swift

    func reloadFromOrigin() -> WKNavigation?

    Objective-C

    - (WKNavigation *)reloadFromOrigin

    Return Value

    A new navigation representing the reload.

    Availability

    Available in iOS 8.0 and later.

  • Stops loading all resources on the current page.

    Declaration

    Swift

    func stopLoading()

    Objective-C

    - (void)stopLoading

    Availability

    Available in iOS 8.0 and later.

  • Sets the webpage contents and base URL.

    Declaration

    Swift

    func loadData(_ data: NSData, MIMEType MIMEType: String, characterEncodingName characterEncodingName: String, baseURL baseURL: NSURL) -> WKNavigation?

    Objective-C

    - (WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL

    Parameters

    data

    The data to use as the contents of the webpage.

    MIMEType

    The MIME type of the data.

    characterEncodingName

    The data's character encoding name.

    baseURL

    A URL used to resolve relative URLs within the document.

    Return Value

    A new navigation.

    Availability

    Available in iOS 9.0 and later.

  • Navigates to the requested file URL on the filesystem

    Declaration

    Swift

    func loadFileURL(_ URL: NSURL, allowingReadAccessToURL readAccessURL: NSURL) -> WKNavigation?

    Objective-C

    - (WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL

    Parameters

    URL

    The file URL to navigate to.

    readAccessURL

    The URL to allow read access to.

    Return Value

    A new navigation for the given file URL.

    Discussion

    If readAccessURL references a single file, only that file may be loaded by WebKit. If readAccessURL references a directory, files inside that file may be loaded by WebKit.

    Availability

    Available in iOS 9.0 and later.

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

    Declaration

    Swift

    var allowsBackForwardNavigationGestures: Bool

    Objective-C

    @property(nonatomic) BOOL allowsBackForwardNavigationGestures

    Discussion

    The default value is NOfalse.

    Availability

    Available in iOS 8.0 and later.

  • The web view's back-forward list. (read-only)

    Declaration

    Swift

    var backForwardList: WKBackForwardList { get }

    Objective-C

    @property(nonatomic, readonly, strong) WKBackForwardList *backForwardList

    Availability

    Available in iOS 8.0 and later.

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

    Declaration

    Swift

    var canGoBack: Bool { get }

    Objective-C

    @property(nonatomic, readonly) BOOL canGoBack

    Availability

    Available in iOS 8.0 and later.

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

    Declaration

    Swift

    var canGoForward: Bool { get }

    Objective-C

    @property(nonatomic, readonly) BOOL canGoForward

    Availability

    Available in iOS 8.0 and later.

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

    Declaration

    Swift

    var allowsLinkPreview: Bool

    Objective-C

    @property(nonatomic) BOOL allowsLinkPreview

    Discussion

    In iOS this property is available on devices that support 3D Touch. Default value is NOfalse in iOS.

    If you set this property’s value to YEStrue, an iOS user can press links to preview link destinations and detected data such as addresses and phone numbers. Such previews are known to users as peeks. If a user presses deeper on a link preview, the preview navigates (or pops, in user terminology) to the destination. Because pop navigation switches the user from your app to Safari, it is opt-in for iOS apps.

    If you want to support link preview in iOS but also want to keep users within your app, you can switch from using the WKWebView class to the SFSafariViewController class. If you are using a web view as an in-app browser, making this change is best practice. The Safari view controller class automatically supports link previews.

    In OS X this property is available on devices with a Force Touch trackpad. Default value is YEStrue in OS X.

    With this property set to its default value of YEStrue, an OS X user can force click a link to display a preview window showing the link’s destination. If the user then clicks on the preview, the destination opens in Safari.

    On both platforms, all types of detected data respond to presses when this property is set to YEStrue. That is, in iOS 9 and OS X v10.11, you cannot specify which types of data are detected in the WKWebView class.

    Availability

    Available in iOS 9.0 and later.

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

    Declaration

    Swift

    func goBack() -> WKNavigation?

    Objective-C

    - (WKNavigation *)goBack

    Return Value

    A new navigation to the requested item, or nil if there is no back item in the back-forward list.

    Availability

    Available in iOS 8.0 and later.

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

    Declaration

    Swift

    func goForward() -> WKNavigation?

    Objective-C

    - (WKNavigation *)goForward

    Return Value

    A new navigation to the requested item, or nil if there is no forward item in the back-forward list.

    Availability

    Available in iOS 8.0 and later.

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

    Declaration

    Swift

    func goToBackForwardListItem(_ item: WKBackForwardListItem) -> WKNavigation?

    Objective-C

    - (WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item

    Parameters

    item

    The item to navigate to. The item must be one of the items in the web view’s back-forward list.

    Return Value

    A new navigation to the requested item, or nil if it is already the current item or is not part of the web view's back-forward list.

    Availability

    Available in iOS 8.0 and later.

  • Navigates to a requested URL.

    Declaration

    Swift

    func loadRequest(_ request: NSURLRequest) -> WKNavigation?

    Objective-C

    - (WKNavigation *)loadRequest:(NSURLRequest *)request

    Parameters

    request

    The request specifying the URL to navigate to.

    Return Value

    A new navigation for the given request.

    Availability

    Available in iOS 8.0 and later.

  • Evaluates a JavaScript string.

    Declaration

    Swift

    func evaluateJavaScript(_ javaScriptString: String, completionHandler completionHandler: ((AnyObject?, NSError?) -> Void)?)

    Objective-C

    - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *error))completionHandler

    Parameters

    javaScriptString

    The JavaScript string to evaluate.

    completionHandler

    A block to invoke when script evaluation completes or fails.

    Discussion

    The method sends the result of the script evaluation (or an error) to the completion handler.

    Availability

    Available in iOS 8.0 and later.