Class

UIWeb​View

You can use the UIWeb​View class to embed web content in your app. To do so, create a UIWeb​View object, attach it to a window, and send it a request to load web content. You can also use this class to move back and forward in the history of webpages, and you can even set some web content properties programmatically.

Overview

Use the load​HTMLString(_:​base​URL:​) method to begin loading local HTML files or the load​Request(_:​) method to begin loading web content. Use the stop​Loading() method to stop loading, and the is​Loading property to find out if a web view is in the process of loading.

If you allow the user to move back and forward through the webpage history, then you can use the go​Back() and go​Forward() methods as actions for buttons. Use the can​Go​Back and can​Go​Forward 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 data​Detector​Types property with a UIData​Detector​Types bitfield that does not contain the phone​Number flag.

You can also use the scales​Page​To​Fit property 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.

Set the delegate property to an object conforming to the UIWeb​View​Delegate protocol if you want to track the loading of web content.

You can easily debug the HTML, CSS, and JavaScript contained inside a UIWeb​View with Web Inspector. Read Debugging Web Content on iOS to learn how to configure Web Inspector for iOS. Read the rest of Safari Web Content Guide to learn how to create web content that is optimized for Safari on iPhone and iPad.

For information about basic view behaviors, see View Programming Guide for iOS.

Supported File Formats

In addition to HTML content, UIWeb​View objects can be used to display other content types, such as Keynote, PDF, and Pages documents. For the best rendering of plain and rich text in your app, however, you should use UIText​View instead.

State Preservation

In iOS 6 and later, if you assign a value to this view’s restoration​Identifier property, it attempts to preserve its URL history, the scaling and scrolling positions for each page, and information about which page is currently being viewed. During restoration, the view restores these values so that the web content appears just as it did before. For more information about how state preservation and restoration works, see App Programming Guide for iOS.

For more information about appearance and behavior configuration, see Web Views.

Subclassing Notes

The UIWeb​View class should not be subclassed.

Symbols

Setting the Delegate

Loading Content

func load(Data, mime​Type:​ String, text​Encoding​Name:​ String, base​URL:​ URL)

Sets the main page contents, MIME type, content encoding, and base URL.

func load​HTMLString(String, base​URL:​ URL?)

Sets the main page content and base URL.

func load​Request(URLRequest)

Connects to a given URL by initiating an asynchronous client request.

var request:​ URLRequest?

The URL request identifying the location of the content to load.

var is​Loading:​ Bool

A Boolean value indicating whether the receiver is done loading content.

func stop​Loading()

Stops the loading of any web content managed by the receiver.

func reload()

Reloads the current page.

Moving Back and Forward

var can​Go​Back:​ Bool

A Boolean value indicating whether the receiver can move backward.

var can​Go​Forward:​ Bool

A Boolean value indicating whether the receiver can move forward.

func go​Back()

Loads the previous location in the back-forward list.

func go​Forward()

Loads the next location in the back-forward list.

Setting Web Content Properties

var allows​Link​Preview:​ Bool

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

var scales​Page​To​Fit:​ Bool

A Boolean value determining whether the webpage scales to fit the view and the user can change the scale.

var scroll​View:​ UIScroll​View

The scroll view associated with the web view.

var suppresses​Incremental​Rendering:​ Bool

A Boolean value indicating whether the web view suppresses content rendering until it is fully loaded into memory.

var keyboard​Display​Requires​User​Action:​ Bool

A Boolean value indicating whether web content can programmatically display the keyboard.

var data​Detector​Types:​ UIData​Detector​Types

The types of data converted to clickable URLs in the web view’s content.

Running JavaScript

func string​By​Evaluating​Java​Script(from:​ String)

Returns the result of running a JavaScript script. Although this method is not deprecated, best practice is to use the evaluate​Java​Script(_:​completion​Handler:​) method of the WKWeb​View class instead.

Managing Media Playback

var allows​Inline​Media​Playback:​ Bool

A Boolean value that determines whether HTML5 videos play inline or use the native full-screen controller.

var media​Playback​Requires​User​Action:​ Bool

A Boolean value that determines whether HTML5 videos can play automatically or require the user to start playing them.

var media​Playback​Allows​Air​Play:​ Bool

A Boolean value that determines whether Air Play is allowed from this view.

var allows​Picture​In​Picture​Media​Playback:​ Bool

A Boolean value that determines whether Picture in Picture playback is allowed from this view.

Managing Pages

var gap​Between​Pages:​ CGFloat

The size of the gap, in points, between pages.

var page​Count:​ Int

The number of pages produced by the layout of the web view.

var page​Length:​ CGFloat

The size of each page, in points, in the direction that the pages flow.

var pagination​Breaking​Mode:​ UIWeb​Pagination​Breaking​Mode

The manner in which column- or page-breaking occurs.

var pagination​Mode:​ UIWeb​Pagination​Mode

The layout of content in the web view.

Constants

UIWeb​View​Navigation​Type

Constant indicating the user’s action.

UIWeb​Pagination​Breaking​Mode

The manner in which column- or page-breaking occurs.

UIWeb​Pagination​Mode

The layout of content in the web view, which determines the direction that the pages flow.