Class

UIWebView

A view that embeds web content in your app.

Overview

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.

If you allow the user to move back and forward through the webpage history, then you can 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 UIDataDetectorTypes bitfield that does not contain the UIDataDetectorTypePhoneNumber flag.

You can also use the scalesPageToFit 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 UIWebViewDelegate protocol if you want to track the loading of web content.

You can easily debug the HTML, CSS, and JavaScript contained inside a UIWebView 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, UIWebView 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 UITextView instead.

State Preservation

In iOS 6 and later, if you assign a value to this view’s restorationIdentifier 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 UIWebView class should not be subclassed.

Topics

Responding to Web View Changes

delegate

The receiver’s delegate.

UIWebViewDelegate

The UIWebViewDelegate protocol defines methods that a delegate of a UIWebView object can optionally implement to intervene when web content is loaded.

Loading Content

loadData:MIMEType:textEncodingName:baseURL:

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

loadHTMLString:baseURL:

Sets the main page content and base URL.

loadRequest:

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

request

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

loading

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

stopLoading

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

reload

Reloads the current page.

Moving Back and Forward

canGoBack

A Boolean value indicating whether the receiver can move backward.

canGoForward

A Boolean value indicating whether the receiver can move forward.

goBack

Loads the previous location in the back-forward list.

goForward

Loads the next location in the back-forward list.

Setting Web Content Properties

allowsLinkPreview

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

scalesPageToFit

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

scrollView

The scroll view associated with the web view.

suppressesIncrementalRendering

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

keyboardDisplayRequiresUserAction

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

dataDetectorTypes

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

Running JavaScript

stringByEvaluatingJavaScriptFromString:

Returns the result of running a JavaScript script. Although this method is not deprecated, best practice is to use the evaluateJavaScript:completionHandler: method of the WKWebView class instead.

Managing Media Playback

allowsInlineMediaPlayback

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

mediaPlaybackRequiresUserAction

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

mediaPlaybackAllowsAirPlay

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

allowsPictureInPictureMediaPlayback

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

Managing Pages

gapBetweenPages

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

pageCount

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

pageLength

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

paginationBreakingMode

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

paginationMode

The layout of content in the web view.

Deprecated Properties

detectsPhoneNumbers

A Boolean value indicating whether telephone number detection is on.

Deprecated

Constants

UIWebViewNavigationType

Constant indicating the user’s action.

UIWebPaginationBreakingMode

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

UIWebPaginationMode

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

UIDataDetectorTypes

Defines the types of information that can be detected in text-based content.

Relationships

Inherits From

See Also

Content Views

UIActivityIndicatorView

A view that shows that a task is in progress.

UIImageView

An object that displays a single image or a sequence of animated images in your interface.

UIPickerView

A view that uses a spinning-wheel or slot-machine metaphor to show one or more sets of values.

UIProgressView

A view that depicts the progress of a task over time.