Skip to content

Add WebRequestInterceptResult.Respond for custom URL scheme support#402

Open
sebastiangl wants to merge 3 commits intoKevinnZou:mainfrom
sebastiangl:feature/custom-url-scheme-respond
Open

Add WebRequestInterceptResult.Respond for custom URL scheme support#402
sebastiangl wants to merge 3 commits intoKevinnZou:mainfrom
sebastiangl:feature/custom-url-scheme-respond

Conversation

@sebastiangl
Copy link
Contributor

This adds the ability to respond with custom data from a RequestInterceptor, enabling implementation of custom URL schemes (e.g., app://, local://).

Changes:

  • Add WebRequestInterceptResult.Respond class with data, mimeType, statusCode, headers
  • Add WKSchemeHandler (iOS) implementing WKURLSchemeHandlerProtocol in Kotlin/Native
  • Add PlatformWebViewParams.customSchemes parameter (iOS) to register custom schemes
  • Handle Respond result in all platforms (iOS, Android, Desktop)

This addresses the feature request from issue #180 for shouldInterceptRequest-like functionality that allows returning custom responses.

This adds the ability to respond with custom data from a RequestInterceptor,
enabling implementation of custom URL schemes (e.g., app://, local://).

Changes:
- Add WebRequestInterceptResult.Respond class with data, mimeType, statusCode, headers
- Add WKSchemeHandler (iOS) implementing WKURLSchemeHandlerProtocol in Kotlin/Native
- Add PlatformWebViewParams.customSchemes parameter (iOS) to register custom schemes
- Handle Respond result in all platforms (iOS, Android, Desktop)

This addresses the feature request from issue KevinnZou#180 for shouldInterceptRequest-like
functionality that allows returning custom responses.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings January 29, 2026 08:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for custom URL schemes by introducing the WebRequestInterceptResult.Respond class, which allows request interceptors to return custom response data. This addresses issue #180's request for shouldInterceptRequest-like functionality.

Changes:

  • Added WebRequestInterceptResult.Respond class to enable custom responses with data, mimeType, statusCode, and headers
  • Implemented iOS support via WKSchemeHandler class that implements WKURLSchemeHandlerProtocol
  • Added customSchemes parameter to iOS PlatformWebViewParams to register custom URL schemes
  • Added placeholder handling for Respond result in Android, Desktop, and iOS navigation delegate (with warnings that it's not supported in those contexts)

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
webview/src/commonMain/kotlin/com/multiplatform/webview/request/WebRequestInterceptResult.kt Defines the new Respond class for custom response handling
webview/src/iosMain/kotlin/com/multiplatform/webview/request/WKSchemeHandler.kt New iOS implementation of WKURLSchemeHandlerProtocol for intercepting custom scheme requests
webview/src/iosMain/kotlin/com/multiplatform/webview/web/WebView.ios.kt Adds PlatformWebViewParams with customSchemes and registers scheme handlers
webview/src/iosMain/kotlin/com/multiplatform/webview/web/WKNavigationDelegate.kt Adds handling for Respond in navigation delegate (logs warning and cancels)
webview/src/androidMain/kotlin/com/multiplatform/webview/web/AccompanistWebView.kt Adds placeholder handling for Respond in shouldOverrideUrlLoading
webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebEngineExt.kt Adds placeholder handling for Respond (logs warning and blocks request)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +434 to +437
is WebRequestInterceptResult.Respond -> {
// Respond is handled in shouldInterceptRequest, not here
KLogger.w { "Respond interceptResult not supported in shouldOverrideUrlLoading" }
true
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

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

The comment states "Respond is handled in shouldInterceptRequest, not here", but the shouldInterceptRequest method (lines 323-332) doesn't actually implement handling for WebRequestInterceptResult.Respond. It only delegates to assetLoader and doesn't check the request interceptor or handle custom responses.

The shouldInterceptRequest method needs to be updated to:

  1. Call navigator.requestInterceptor?.onInterceptUrlRequest()
  2. When the result is WebRequestInterceptResult.Respond, create and return a WebResourceResponse with the response data, mimeType, statusCode, and headers
  3. This is critical for Android support of custom URL schemes as mentioned in issue Add shouldInterceptRequest method #180

Without this implementation, the Respond functionality will not work on Android.

Copilot uses AI. Check for mistakes.
Fixes based on Copilot review comments:

WKSchemeHandler.kt:
- Remove unused HTTPMethod import
- Add try-catch around interceptor call with proper cleanup in finally block
- Fix task cancellation to call failTask before returning
- Use mainDocumentURL heuristic for isForMainFrame detection
- Add null-safety for NSURL.URLWithString and NSHTTPURLResponse
- Fix Content-Type header precedence (mimeType takes priority)
- Add thread-safety documentation comment

WebView.ios.kt:
- Use remember(navigator) to recreate handler when navigator changes
- Add reserved schemes validation (http, https, file, ftp, about, data, javascript)
- Improve documentation for customSchemes parameter

WebRequestInterceptResult.kt:
- Update documentation to clarify platform support (iOS only for now)
- Document that mimeType takes precedence over Content-Type header

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@sebastiangl sebastiangl force-pushed the feature/custom-url-scheme-respond branch from f778b5f to 8f348bd Compare January 29, 2026 19:17
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants