add
NewiOSmacOSvisionOSopen func add(_ buffer: WKJSScriptingBuffer, name: String, contentWorld world: WKContentWorld)What's New / App UI: SwiftUI, AppKit & UIKit
WebKit is Apple's web rendering engine, exposed through WKWebView and its delegate protocols for loading, navigating, and scripting web content. It also handles cookies and JavaScript message handlers.
The 27 SDK adds 39 APIs, with no deprecations or removals. New types cover an immersive-environment path for web content: ImmersiveEnvironment, WebViewImmersiveEnvironmentView, the WKImmersiveEnvironmentDelegate protocol, and a FormInfo struct. Other additions include getCookies, world, nodeSerializationEnabled, jsHandleCreationEnabled, mainFrameNavigation, and new webView overloads on WKNavigationDelegate and WKUIDelegate.
addopen func add(_ buffer: WKJSScriptingBuffer, name: String, contentWorld world: WKContentWorld)allowsImmersiveEnvironmentsopen var allowsImmersiveEnvironments: BoolallowsJSHandleCreationInPageWorldopen var allowsJSHandleCreationInPageWorld: BoolalternateRequestopen var alternateRequest: URLRequest?dismissImmersiveEnvironmentopen func dismissImmersiveEnvironment(completionHandler: @escaping @MainActor @Sendable () -> Void)dismissImmersiveEnvironmentopen func dismissImmersiveEnvironment() asyncgetCookiesopen func getCookies(for url: URL, completionHandler: @escaping @MainActor @Sendable ([HTTPCookie]) -> Void)getCookiesopen func getCookies(for url: URL) async -> [HTTPCookie]immersiveEnvironmentDelegateweak open var immersiveEnvironmentDelegate: (any WKImmersiveEnvironmentDelegate)?jsHandleCreationEnabledopen var jsHandleCreationEnabled: Boolloadopen func load(_ url: URL) -> WKNavigation?mainFrameNavigationopen var mainFrameNavigation: WKNavigation? { get }nodeSerializationEnabledopen var nodeSerializationEnabled: BooloverrideReferreropen var overrideReferrer: String?refreshControlleropen var refreshController: NSRefreshController?removeBufferopen func removeBuffer(withName name: String, contentWorld world: WKContentWorld)WebPage.FormInfoextension WebPage.FormInfo : Sendableextension WebPage.FormInfo : Sendable {
}WebViewImmersiveEnvironmentViewpublic struct WebViewImmersiveEnvironmentView : ViewA SwiftUI view that renders a specific website-provided immersive environment.
Place this view in your app's Immersive Space hierarchy. Initialize it with the WebPage.ImmersiveEnvironment received from the presentation callback to render that specific environment.
@MainActor public struct WebViewImmersiveEnvironmentView : View {
/// Creates an immersive environment view from a ``WebPage/ImmersiveEnvironment``.
@MainActor public init(_ environment: WebPage.ImmersiveEnvironment)
/// Creates an immersive environment view from a `WKImmersiveEnvironment`.
@MainActor public init(_ environment: WKImmersiveEnvironment)
/// The content and behavior of the view.
///
/// When you implement a custom view, you must implement a computed
/// `body` property to provide the content for your view. Return a view
/// that's composed of built-in views that SwiftUI provides, plus other
/// composite views that you've already defined:
///
/// struct MyView: View {
/// var body: some View {
/// Text("Hello, World!")
/// }
/// }
///
/// For more information about composing views and a view hierarchy,
/// see <doc:Declaring-a-Custom-View>.
@MainActor @preconcurrency public var body: some View { get }
/// The type of view representing the body of this view.
///
/// When you create a custom view, Swift infers this type from your
/// implementation of the required ``View/body-swift.property`` property.
@available(visionOS 27.0, *)
@available(macOS, unavailable, introduced: 9999)
@available(iOS, unavailable, introduced: 9999)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
public typealias Body = some View
}WebViewImmersiveEnvironmentViewextension WebViewImmersiveEnvironmentView : Sendableextension WebViewImmersiveEnvironmentView : Sendable {
}WKImmersiveEnvironmentDelegatepublic protocol WKImmersiveEnvironmentDelegate : NSObjectProtocol@MainActor public protocol WKImmersiveEnvironmentDelegate : NSObjectProtocol {
/** @abstract Asks the delegate whether to allow an immersive environment from the specified frame.
@param webView The web view that received the immersive environment request.
@param frame The frame information from the website requesting the immersive environment.
@param completionHandler The completion handler you must invoke with the request's answer. `YES` to allow
the environment presentation, or `NO` to deny it.
*/
@available(visionOS 27.0, *)
func webView(_ webView: WKWebView, shouldAllowImmersiveEnvironmentFromFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void)
/** @abstract Asks the delegate whether to allow an immersive environment from the specified frame.
@param webView The web view that received the immersive environment request.
@param frame The frame information from the website requesting the immersive environment.
@param completionHandler The completion handler you must invoke with the request's answer. `YES` to allow
the environment presentation, or `NO` to deny it.
*/
@available(visionOS 27.0, *)
func webView(_ webView: WKWebView, shouldAllowImmersiveEnvironmentFrom frame: WKFrameInfo) async -> Bool
/** @abstract Asks the delegate to present an immersive environment.
@param webView The web view requesting presentation.
@param environment The immersive environment to present.
@param completionHandler The completion handler you must invoke once the presentation transition has completed.
The error argument should be used in case the presentation failed and the environment couldn't be presented.
*/
@available(visionOS 27.0, *)
func webView(_ webView: WKWebView, present environment: WKImmersiveEnvironment, completionHandler: @escaping ((any Error)?) -> Void)
/** @abstract Asks the delegate to present an immersive environment.
@param webView The web view requesting presentation.
@param environment The immersive environment to present.
@param completionHandler The completion handler you must invoke once the presentation transition has completed.
The error argument should be used in case the presentation failed and the environment couldn't be presented.
*/
@available(visionOS 27.0, *)
func webView(_ webView: WKWebView, presentImmersiveEnvironment environment: WKImmersiveEnvironment) async throws
/** @abstract Asks the delegate to dismiss an immersive environment.
@param webView The web view requesting dismissal.
@param environment The immersive environment to dismiss.
@param completionHandler The completion handler you must invoke once the dismissal transition has completed.
*/
@available(visionOS 27.0, *)
func webView(_ webView: WKWebView, dismiss environment: WKImmersiveEnvironment, completionHandler: @escaping () -> Void)
/** @abstract Asks the delegate to dismiss an immersive environment.
@param webView The web view requesting dismissal.
@param environment The immersive environment to dismiss.
@param completionHandler The completion handler you must invoke once the dismissal transition has completed.
*/
@available(visionOS 27.0, *)
func webView(_ webView: WKWebView, dismissImmersiveEnvironment environment: WKImmersiveEnvironment) async
}worldopen class func world(with configuration: WKContentWorldConfiguration) -> WKContentWorldView.onWebViewImmersiveEnvironmentRequestnonisolated public func onWebViewImmersiveEnvironmentRequest(shouldAllow: @escaping @MainActor @Sendable (_ sourceFrame: WebPage.FrameInfo) async -> Bool, present: @escaping @MainActor @Sendable (_ environment: WebPage.ImmersiveEnvironment) async throws -> Void, dismiss: @escaping @MainActor @Sendable (_ environment: WebPage.ImmersiveEnvironment) async -> Void) -> some ViewManages the lifecycle of immersive environments requested by websites.
Use this modifier to control authorization, presentation, and dismissal of immersive environments from websites.
Parameters
shouldAllowWebPage.FrameInfo and should return true to allow the environment presentation, or false to deny it.presentWebPage.ImmersiveEnvironment. Use this to open an Immersive Space containing a WebViewImmersiveEnvironmentView initialized with this environment. If another immersive space is already being presented, dismiss it first. This closure should return after the presentation transition completes.dismissWebPage.ImmersiveEnvironment to dismiss. This closure should return after the dismissal transition completes.ReturnsA modified view that manages immersive environment lifecycle.
WebPage.FormInfopublic struct FormInfoA type that contains information about a form submission from a webpage.
@MainActor public struct FormInfo {
/// The frame where the form is being submitted will cause a navigation.
@MainActor public var targetFrame: WebPage.FrameInfo { get }
/// The frame that caused the form submission.
@MainActor public var sourceFrame: WebPage.FrameInfo { get }
/// The URL that the frame is being navigated to.
@MainActor public var submissionURL: URL { get }
/// The HTTP method used to submit the form; generally either @"GET" or @"POST".
@MainActor public var httpMethod: String { get }
/// A dictionary of the form values that will be submitted during the navigation.
@MainActor public var formValues: [String : String] { get }
}WebPage.ImmersiveEnvironmentpublic struct ImmersiveEnvironment : SendableAn object representing a website-provided immersive environment that is ready for presentation.
@MainActor public struct ImmersiveEnvironment : Sendable {
/// The frame information of the website that provided this immersive environment.
@MainActor public var sourceFrame: WebPage.FrameInfo { get }
}WebPage.Configuration.allowsImmersiveEnvironmentspublic var allowsImmersiveEnvironments: BoolIndicates whether website immersive environments are allowed.
Set this property to true to enable support for website immersive environments. If false, requests to present immersive environments are ignored. If true, requests are routed through the onWebViewImmersiveEnvironmentRequest view modifier callbacks.
The default value is false.
WebPage.NavigationDeciding.willSubmitmutating func willSubmit(formInfo: WebPage.FormInfo) asyncAllow the application to process form autofill information before a form submission actually takes place.
This is an informative callback only. The form values cannot be changed, nor can the navigation be changed to not submit a form.
The form submission will not actually proceed until after this callback asynchronously resolves.
Parameters
formInfoWebPage.NavigationDeciding.willSubmitpublic func willSubmit(formInfo: WebPage.FormInfo) asyncBy default, this method does nothing.
WebPage.NavigationPreferences.alternateRequestpublic var alternateRequest: URLRequest?Used to make changes to the network request that will be used for this navigation's main resource load.
WebPage.NavigationPreferences.overrideReferrerpublic var overrideReferrer: String?Used to apply a custom referer header to all resource loads in the frame of this navigation.
WebViewImmersiveEnvironmentView.Bodypublic typealias Body = some ViewThe type of view representing the body of this view.
When you create a custom view, Swift infers this type from your implementation of the required body-swift.property property.
WKImmersiveEnvironmentDelegate.webViewfunc webView(_ webView: WKWebView, shouldAllowImmersiveEnvironmentFromFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void)WKImmersiveEnvironmentDelegate.webViewfunc webView(_ webView: WKWebView, shouldAllowImmersiveEnvironmentFrom frame: WKFrameInfo) async -> BoolWKImmersiveEnvironmentDelegate.webViewfunc webView(_ webView: WKWebView, present environment: WKImmersiveEnvironment, completionHandler: @escaping ((any Error)?) -> Void)WKImmersiveEnvironmentDelegate.webViewfunc webView(_ webView: WKWebView, presentImmersiveEnvironment environment: WKImmersiveEnvironment) async throwsWKImmersiveEnvironmentDelegate.webViewfunc webView(_ webView: WKWebView, dismiss environment: WKImmersiveEnvironment, completionHandler: @escaping () -> Void)WKImmersiveEnvironmentDelegate.webViewfunc webView(_ webView: WKWebView, dismissImmersiveEnvironment environment: WKImmersiveEnvironment) asyncWKNavigationDelegate.webViewoptional func webView(_ webView: WKWebView, willSubmitForm formInfo: WKFormInfo, submissionHandler: @escaping @MainActor @Sendable () -> Void)WKNavigationDelegate.webViewoptional func webView(_ webView: WKWebView, willSubmitForm formInfo: WKFormInfo) asyncWKUIDelegate.webViewoptional func webView(_ webView: WKWebView, requestGeolocationPermissionFor origin: WKSecurityOrigin, initiatedByFrame frame: WKFrameInfo, decisionHandler: @escaping @MainActor @Sendable (WKPermissionDecision) -> Void)No APIs match your filter.