I am implementing an iOS webview for the first tim...
# compose-ios
d
I am implementing an iOS webview for the first time. I having issues with incercepting clicks on links with custom protocol (e.g. "search://"). On the CMP website, they show the implementation for the navigation delegate, but not for the url scheme handler (which is what you use on the WKWebView): https://kotlinlang.org/docs/multiplatform/compose-uikit-integration.html#web-view In the comment I am attaching my implementation. Let me know if I did anything wrong. ---> UPDATE: solved!
Copy code
@OptIn(ExperimentalResourceApi::class, ExperimentalForeignApi::class)
@Composable
actual fun ResultsButtonPopupWebview(url: String, onWebviewLoaded: () -> Unit, onItemClick: (url: String) -> Unit) {

    var isLoaded by remember { mutableStateOf(false) }

    val delegate = remember {
        object : NSObject(), WKNavigationDelegateProtocol {
            override fun webView(
                webView: WKWebView,
                didFinishNavigation: WKNavigation?
            ) {
                isLoaded = true
                onWebviewLoaded()
            }
        }

    }

    val searchHandler = remember {
        object : NSObject(), WKURLSchemeHandlerProtocol {

            @ObjCSignatureOverride
            override fun webView(
                webView: WKWebView,
                startURLSchemeTask: WKURLSchemeTaskProtocol
            ) {
                println("TEST startURLSchemeTask: " + startURLSchemeTask.request.URL?.absoluteString)
                onItemClick(startURLSchemeTask.request.URL?.absoluteString ?: "")
            }

            @ObjCSignatureOverride
            override fun webView(
                webView: WKWebView,
                stopURLSchemeTask: WKURLSchemeTaskProtocol
            ) {

            }
        }
    }

    UIKitView(
        modifier = Modifier
            .fillMaxSize()
            .alpha(if (isLoaded) 1f else 0f), // hide while loading
        factory = {
            WKWebView().apply {
                navigationDelegate = delegate
                loadRequest(NSURLRequest(uRL = NSURL(string = url)))
                val preferences = WKPreferences()
                preferences.javaScriptEnabled = true
                val config = WKWebViewConfiguration()
                config.preferences = preferences
                config.setURLSchemeHandler(searchHandler, "search")
            }
        },
    )

}
it never reaches the println
Oh!!! I found the reason. the configuration should be passed in the WKWebView constructor!!!
Copy code
UIKitView(
    modifier = Modifier
        .fillMaxSize()
        .alpha(if (isLoaded) 1f else 0f), // hide while loading
    factory = {
        val preferences = WKPreferences()
        preferences.javaScriptEnabled = true
        val config = WKWebViewConfiguration()
        config.preferences = preferences
        config.setURLSchemeHandler(searchHandler, "search")
        WKWebView(frame = CGRectZero.readValue(), configuration = config).apply {
            navigationDelegate = delegate
            loadRequest(NSURLRequest(uRL = NSURL(string = url)))
        }
    },
)