Contents

UIWebView

A view that embeds web content in your app.

Declaration

@MainActor class UIWebView

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 isLoading 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 doesn’t contain the phoneNumber flag.

You can also use the scalesPageToFit property to programmatically set the scale of web content the first time it’s 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 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’s 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.

Subclassing notes

The UIWebView class shouldn’t be subclassed.

Topics

Essentials

Responding to web view changes

Loading content

Moving back and forward

Setting web content properties

Running JavaScript

Managing media playback

Managing pages

Constants

See Also

Deprecated classes