webView(_:decidePolicyFor:preferences:decisionHandler:)
Asks the delegate for permission to navigate to new content based on the specified preferences and action information.
Declaration
optional func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping @MainActor @Sendable (WKNavigationActionPolicy, WKWebpagePreferences) -> Void)optional func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences) async -> (WKNavigationActionPolicy, WKWebpagePreferences)Parameters
- webView:
The web view from which the navigation request began.
- navigationAction:
Details about the action that triggered the navigation request.
- preferences:
The default preferences to use when displaying the new webpage. Specify the default preferences for pages using the Defaultwebpagepreferences property of Wkwebviewconfiguration when you create your web view.
- decisionHandler:
A completion handler block to call with the results about whether to allow or cancel the navigation. This handler has no return value and takes the following parameters:
- policy
A constant that indicates whether to cancel or allow the navigation. For a list of possible values, see Wknavigationactionpolicy.
- preferences
The set of preferences to apply to the page if the navigation is allowed. You may pass the object from the
preferencesparameter or configure a new preferences object and pass it instead.
Discussion
Use this method to allow or deny a navigation request that originated with the specified action. The web view calls this method after the interaction occurs but before it attempts to load any content. If you implement this method, always execute the decisionHandler block at some point. You may execute it synchronously from your delegate method’s implementation, or execute it asynchronously after your method returns.
If your delegate object implements this method, the web view doesn’t call the webView(_:decidePolicyFor:decisionHandler:) method.