Add WebRequestInterceptResult.Respond for custom URL scheme support#402
Add WebRequestInterceptResult.Respond for custom URL scheme support#402sebastiangl wants to merge 3 commits intoKevinnZou:mainfrom
Conversation
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>
There was a problem hiding this comment.
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.Respondclass to enable custom responses with data, mimeType, statusCode, and headers - Implemented iOS support via
WKSchemeHandlerclass that implementsWKURLSchemeHandlerProtocol - Added
customSchemesparameter to iOSPlatformWebViewParamsto register custom URL schemes - Added placeholder handling for
Respondresult 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.
webview/src/iosMain/kotlin/com/multiplatform/webview/request/WKSchemeHandler.kt
Outdated
Show resolved
Hide resolved
webview/src/iosMain/kotlin/com/multiplatform/webview/web/WebView.ios.kt
Outdated
Show resolved
Hide resolved
webview/src/iosMain/kotlin/com/multiplatform/webview/request/WKSchemeHandler.kt
Show resolved
Hide resolved
| is WebRequestInterceptResult.Respond -> { | ||
| // Respond is handled in shouldInterceptRequest, not here | ||
| KLogger.w { "Respond interceptResult not supported in shouldOverrideUrlLoading" } | ||
| true |
There was a problem hiding this comment.
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:
- Call navigator.requestInterceptor?.onInterceptUrlRequest()
- When the result is WebRequestInterceptResult.Respond, create and return a WebResourceResponse with the response data, mimeType, statusCode, and headers
- 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.
webview/src/iosMain/kotlin/com/multiplatform/webview/request/WKSchemeHandler.kt
Outdated
Show resolved
Hide resolved
webview/src/iosMain/kotlin/com/multiplatform/webview/request/WKSchemeHandler.kt
Show resolved
Hide resolved
webview/src/iosMain/kotlin/com/multiplatform/webview/request/WKSchemeHandler.kt
Outdated
Show resolved
Hide resolved
webview/src/iosMain/kotlin/com/multiplatform/webview/web/WebView.ios.kt
Outdated
Show resolved
Hide resolved
webview/src/iosMain/kotlin/com/multiplatform/webview/request/WKSchemeHandler.kt
Outdated
Show resolved
Hide resolved
webview/src/iosMain/kotlin/com/multiplatform/webview/web/WebView.ios.kt
Outdated
Show resolved
Hide resolved
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>
f778b5f to
8f348bd
Compare
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This adds the ability to respond with custom data from a RequestInterceptor, enabling implementation of custom URL schemes (e.g., app://, local://).
Changes:
This addresses the feature request from issue #180 for shouldInterceptRequest-like functionality that allows returning custom responses.