uikitviewcontroller not registering touch actions ...
# compose-ios
k
uikitviewcontroller not registering touch actions code in thread
Copy code
package com.vyro.chatlyMobile.webview


import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.UIKitInteropInteractionMode
import androidx.compose.ui.viewinterop.UIKitInteropProperties
import androidx.compose.ui.viewinterop.UIKitViewController
import kotlinx.cinterop.ExperimentalForeignApi
import platform.Foundation.NSLog
import platform.Foundation.NSURL
import platform.Foundation.NSURLComponents
import platform.Foundation.NSURLQueryItem
import platform.SafariServices.SFSafariViewController
import platform.SafariServices.SFSafariViewControllerConfiguration
import platform.SafariServices.SFSafariViewControllerDelegateProtocol
import platform.darwin.NSObject

@OptIn(ExperimentalComposeUiApi::class, ExperimentalForeignApi::class)
@Composable
actual fun LoginWebView(
    link: String,
    redirectData: (String, String) -> Unit,
    onBackPressed: () -> Unit,
) {
    val configuration = SFSafariViewControllerConfiguration().apply {
        entersReaderIfAvailable = false
        barCollapsingEnabled = true
    }
    var safariViewController = NSURL.URLWithString(link)?.let { nsUrl ->
            SFSafariViewController(nsUrl, configuration)
        }


    LaunchedEffect(Unit) {
        val delegate = object : NSObject(), SFSafariViewControllerDelegateProtocol {
            override fun safariViewController(
                controller: SFSafariViewController, initialLoadDidRedirectToURL: NSURL
            ) {
                NSLog("DEBUG: Redirect detected")
                NSLog("DEBUG: Full URL: ${initialLoadDidRedirectToURL.absoluteString}")

                val urlString = initialLoadDidRedirectToURL.absoluteString ?: return

                if (urlString.contains("localhost", ignoreCase = true)) {
                    var authCode = ""
                    var authState = ""

                    try {
                        val components =
                            NSURLComponents.componentsWithURL(initialLoadDidRedirectToURL, false)
                        components?.queryItems?.forEach { queryItem ->
                            (queryItem as? NSURLQueryItem)?.let { item ->
                                NSLog("DEBUG: Query Item - Name: ${item.name}, Value: ${item.value}")
                                when (item.name) {
                                    "paymentAuthCode" -> {
                                        authCode = item.value ?: ""
                                        NSLog("DEBUG: Found authCode: $authCode")
                                    }

                                    "paymentAuthState" -> {
                                        authState = item.value ?: ""
                                        NSLog("DEBUG: Found authState: $authState")
                                    }
                                }
                            }
                        }

                        if (authCode.isNotEmpty() && authState.isNotEmpty()) {
                            NSLog("DEBUG: Final values - AuthState: $authState, AuthCode: $authCode")
                            redirectData(authState, authCode)
                        }
                    } catch (e: Exception) {
                        NSLog("DEBUG: Error parsing URL: ${e.message}")
                    }
                }
            }

            override fun safariViewControllerDidFinish(controller: SFSafariViewController) {
                NSLog("DEBUG: Safari controller did finish")
                onBackPressed()
            }
        }
        safariViewController?.delegate = delegate

    }

    UIKitViewController(
        factory = { safariViewController!! },
        modifier = Modifier.fillMaxSize(),
        properties = UIKitInteropProperties(
            interactionMode = UIKitInteropInteractionMode.NonCooperative
        )
    )
}
a
didn't get your problem - SFSafariViewControllerConfiguration does not react to touches?
and you defenetely should
remember
the SFSafariViewController
👍 1
k
Na, uikitviewcontroller does not respond to touches
a
You cannot use SFSafariViewController inside Compose UI. Please check Apple documentation about it: https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller?language=objc#Presenting-the-view-controller-in-your-interface You have to either present it, or use WKWebView to embed it inside Compose.