init(frame: CGRect, configuration: WKWebViewConfiguration )
init?(coder: NSCoder)
var configuration: WKWebViewConfiguration
A WKWeb
object is a platform-native view that you use to incorporate web content seamlessly into your app’s UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app’s native views. Use it when web technologies satisfy your app’s layout and styling requirements more readily than native views. For example, you might use it when your app’s content changes frequently.
A web view offers control over the navigation and user experience through delegate objects. Use the navigation delegate to react when the user clicks links in your web content, or interacts with the content in a way that affects navigation. For example, you might prevent the user from navigating to new content unless specific conditions are met. Use the UI delegate to present native UI elements, such as alerts or contextual menus, in response to interactions with your web content.
Note
WKWeb
replaces the UIWeb
class in iOS 8 and later, and it replaces the Web
class in macOS 10.10 and later.
Embed a WKWeb
object programmatically into your view hierarchy, or add it using Interface Builder. Interface Builder supports many customizations, such as configuring data detectors, media playback, and interaction behaviors. For more extensive customizations, create your web view programmatically using a WKWeb
object. For example, use a web view configuration object to specify handlers for custom URL schemes, manage cookies, and customize preferences for your web content.
Before your web view appears onscreen, load content from a web server using a URLRequest
structure or load content directly from a local file or HTML string. The web view automatically loads embedded resources such as images or videos as part of the initial load request. It then renders your content and displays the results inside the view’s bounds rectangle. The following code example shows a view controller that replaces its default view with a custom WKWeb
object.
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)
}
}
A web view automatically converts telephone numbers that appear in web content to Phone links. When the user taps a Phone link, the Phone app launches and dials the number. Use the WKWeb
object to change the default data detector behavior.
You can also use set
to programmatically set the scale of web content the first time it appears in a web view. Thereafter, the user can change the scale using gestures.
WKWeb
provides a complete browsing experience, including the ability to navigate between different webpages using links, forward and back buttons, and more. When the user clicks a link in your content, the web view acts like a browser and displays the content at that link. To disallow navigation, or to customize your web view’s navigation behavior, provide your web view with a navigation delegate — an object that conforms to the WKNavigation
protocol. Use your navigation delegate to modify the web view’s navigation behavior, or to track the loading progress of new content.
You can also use the methods of WKWeb
to navigate programmatically through your content, or to trigger navigation from other parts of your app’s interface. For example, if your UI includes forward and back buttons, connect those buttons to the go
and go
methods of your web view to trigger the corresponding web navigation. Use the can
and can
properties to determine when to enable or disable your buttons.
People may want to share the contents of your web view with an app or other people. Use a UIActivity
to present a share sheet offering all the ways people can share the web content.
If your app has the com
entitlement, the iOS share sheet can offer Add to Home Screen for an http
or https
webpage, creating a convenient link to a web app or bookmark. To allow someone to add the current webpage to the Home Screen, include the WKWeb
instance in the activity
array when you call init(activity
to create the UIActivity
. For more information about building a browser app, see Preparing your app to be the default web browser.
init(frame: CGRect, configuration: WKWebViewConfiguration )
init?(coder: NSCoder)
var configuration: WKWebViewConfiguration
class func handlesURLScheme (String) -> Bool
var uiDelegate : (any WKUIDelegate)?
protocol WKUIDelegate
var navigationDelegate : (any WKNavigationDelegate )?
protocol WKNavigationDelegate
func load(URLRequest) -> WKNavigation?
func load(Data, mimeType : String, characterEncodingName : String, baseURL : URL) -> WKNavigation?
func loadHTMLString (String, baseURL : URL?) -> WKNavigation?
func loadFileRequest (URLRequest, allowingReadAccessTo : URL) -> WKNavigation
func loadFileURL (URL, allowingReadAccessTo : URL) -> WKNavigation?
func loadSimulatedRequest (URLRequest, response: URLResponse, responseData : Data) -> WKNavigation
func loadSimulatedRequest (URLRequest, responseHTML : String) -> WKNavigation
var isLoading : Bool
var estimatedProgress : Double
func reload() -> WKNavigation?
func reload(Any?)
func reloadFromOrigin () -> WKNavigation?
func reloadFromOrigin (Any?)
func stopLoading ()
func stopLoading (Any?)
func startDownload (using: URLRequest, completionHandler : (WKDownload) -> Void)
func resumeDownload (fromResumeData : Data, completionHandler : (WKDownload) -> Void)
var isInspectable : Bool
var scrollView : UIScrollView
var title: String?
var url: URL?
var mediaType : String?
var customUserAgent : String?
var serverTrust : SecTrust ?
var hasOnlySecureContent : Bool
var themeColor : UIColor?
var underPageBackgroundColor : UIColor!
var pageZoom : CGFloat
var allowsMagnification : Bool
var magnification: CGFloat
func setMagnification (CGFloat, centeredAt : CGPoint)
func pauseAllMediaPlayback (completionHandler : (() -> Void)?)
func requestMediaPlaybackState (completionHandler : (WKMediaPlaybackState ) -> Void)
func setAllMediaPlaybackSuspended (Bool, completionHandler : (() -> Void)?)
func closeAllMediaPresentations (completionHandler : (() -> Void)?)
enum WKMediaPlaybackState
var cameraCaptureState : WKMediaCaptureState
var microphoneCaptureState : WKMediaCaptureState
func setCameraCaptureState (WKMediaCaptureState , completionHandler : (() -> Void)?)
func setMicrophoneCaptureState (WKMediaCaptureState , completionHandler : (() -> Void)?)
enum WKMediaCaptureState
func find(String, configuration: WKFindConfiguration , completionHandler : (WKFindResult ) -> Void)
func find(String, configuration: WKFindConfiguration ) async throws -> WKFindResult
class WKFindConfiguration
class WKFindResult
var allowsBackForwardNavigationGestures : Bool
var backForwardList : WKBackForwardList
func goBack (Any?)
func goBack () -> WKNavigation?
func goForward (Any?)
func goForward () -> WKNavigation?
func go(to: WKBackForwardListItem ) -> WKNavigation?
var canGoBack : Bool
var canGoForward : Bool
var allowsLinkPreview : Bool
var interactionState : Any?
func evaluateJavaScript (String, completionHandler : ((Any?, (any Error)?) -> Void)?)
func evaluateJavaScript (String, in: WKFrameInfo ?, in: WKContentWorld , completionHandler : ((Result<Any, any Error>) -> Void)?)
func evaluateJavaScript (String, in: WKFrameInfo ?, contentWorld : WKContentWorld ) async throws -> Any?
func callAsyncJavaScript (String, arguments: [String : Any], in: WKFrameInfo ?, in: WKContentWorld , completionHandler : ((Result<Any, any Error>) -> Void)?)
func callAsyncJavaScript (String, arguments: [String : Any], in: WKFrameInfo ?, contentWorld : WKContentWorld ) async throws -> Any?
func takeSnapshot (with: WKSnapshotConfiguration ?, completionHandler : (UIImage?, (any Error)?) -> Void)
func createPDF (configuration: WKPDFConfiguration, completionHandler : (Result<Data, any Error>) -> Void)
func pdf(configuration: WKPDFConfiguration) async throws -> Data
func createWebArchiveData (completionHandler : (Result<Data, any Error>) -> Void)
func printOperation (with: NSPrintInfo ) -> NSPrintOperation
class WKSnapshotConfiguration
class WKPDFConfiguration
CALayerDelegate
CVarArg
Copyable
CustomDebugStringConvertible
CustomStringConvertible
Equatable
Hashable
NSAccessibilityElementProtocol
NSAccessibilityProtocol
NSAnimatablePropertyContainer
NSAppearanceCustomization
NSCoding
NSDraggingDestination
NSObjectProtocol
NSStandardKeyBindingResponding
NSTextFinderClient
NSTouchBarProvider
NSUserActivityRestoring
NSUserInterfaceItemIdentification
NSUserInterfaceValidations
UIAccessibilityIdentification
UIActivityItemsConfigurationProviding
UIAppearance
UIAppearanceContainer
UICoordinateSpace
UIDynamicItem
UIFocusEnvironment
UIFocusItem
UIFocusItemContainer
UILargeContentViewerItem
UIPasteConfigurationSupporting
UIPopoverPresentationControllerSourceItem
UIResponderStandardEditActions
UITraitChangeObservable
UITraitEnvironment
UIUserActivityRestoring
protocol WKUIDelegate