Skip to content

[webview_flutter_wkwebview] Add support for custom URL scheme handlers#12236

Open
brlumen wants to merge 2 commits into
flutter:mainfrom
brlumen:pr/wkwebview-url-scheme-handler
Open

[webview_flutter_wkwebview] Add support for custom URL scheme handlers#12236
brlumen wants to merge 2 commits into
flutter:mainfrom
brlumen:pr/wkwebview-url-scheme-handler

Conversation

@brlumen

@brlumen brlumen commented Jul 18, 2026

Copy link
Copy Markdown

Adds support for loading resources with custom URL schemes on iOS and macOS by wrapping the native WKURLSchemeHandler API.

New public API:

  • WebKitWebViewControllerCreationParams.urlSchemeHandlers — a map of URL scheme to handler. Handlers are registered at creation time because WKWebViewConfiguration.setURLSchemeHandler only takes effect before the WKWebView is created.
  • WebKitUrlSchemeHandleronStart/onStop callbacks receiving a WebKitUrlSchemeTask.
  • WebKitUrlSchemeTask — exposes the request url/httpMethod and the reply methods didReceiveResponse, didReceiveData, didFinish, and didFail.

Internal changes:

  • New Pigeon ProxyApis: WKURLSchemeHandler (Dart-implemented, modeled after WKScriptMessageHandler) and WKURLSchemeTask; WKWebViewConfiguration.setURLSchemeHandler; constructors for the existing HTTPURLResponse, URLResponse, and NSError ProxyApis (needed to build responses from Dart).
  • Calling a WKURLSchemeTask method after the web view has stopped the task raises NSException. Since the stop notification inherently races in-flight platform channel calls, the native side tracks stopped tasks (via an associated object) and turns late calls into no-ops; the equivalent race on the Dart side (stop arriving while the start handler is still reading the request) is also handled.

Tests:

  • Dart unit tests for the new wrapper API.
  • Native XCTests for the new ProxyApi delegates, including the stopped-task no-op guard.
  • Integration tests in the example app covering: serving an image to a real WKWebView, stopping an in-flight task when the page is replaced (with late replies being no-ops), delivering a large (3 MB) response body via main frame navigation to a custom scheme URL, and 24 concurrent requests on one page.

Example use case: an email client that rewrites cid: inline-image references to a custom scheme and serves the bytes from its local attachment cache. WKWebView cannot intercept http(s): requests, so a custom scheme handler is the platform-sanctioned counterpart of Android's shouldInterceptRequest for serving locally cached subresources.

Part of flutter/flutter#27896 (implements the iOS/macOS side).

Pre-Review Checklist

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2

Adds WebKitWebViewControllerCreationParams.urlSchemeHandlers, wrapping the
native WKURLSchemeHandler API to load resources for custom URL schemes on
iOS and macOS. Also adds constructors for the HTTPURLResponse, URLResponse,
and NSError proxy APIs, needed to respond to scheme tasks from Dart.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements support for custom URL schemes in webview_flutter_wkwebview via the native WKURLSchemeHandler API, introducing WebKitUrlSchemeHandler and WebKitUrlSchemeTask along with their native Swift delegates and Pigeon bindings. The review feedback identifies a potential memory leak in _registerUrlSchemeHandler where tasks can remain in stoppedBeforeStart indefinitely, and recommends setting _stopped to true upon task completion to safeguard against post-completion calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant