Hi there how to make phone number and url intent h...
# multiplatform
s
Hi there how to make phone number and url intent handler in KMM
j
This sounds platform-specific, "intent" is an Android thing, not KMM. Can you tell us a bit more about what you are trying to achieve / how's your app structured? I'm curious because you are asking specifically about intents...
s
When user clicks on number we should open dialer Also when clicks on URL we need to open in default phone browser
j
But what do you use for this UI that it is cross-platform?
s
yes compose
j
I mean - there are multiple ways how to do this - UI in some cross-platform toolkit, or separate UI for Android or iOS, etc.
If I'm not mistaken, with Compose, you have to do this the platform specific way on iOS - so you need to abstract the dialing functionality and do separate Android and iOS implementation.
We are working on much tighter Android-alike API for KMM here: https://crossoid.com/ that directly has Intent and stuff, but not sure if you are interested in a commercial solution.
s
ho I see
yes platform specific I have to write...
sorry no commercial for now
j
No problem 🙂
s
If you just need a url handler you can do this.
Copy code
val uriHandler = LocalUriHandler.current

  LaunchedEffect(Unit) {
    uriHandler.openUri("tel:9999999999")
  }
s
Trying now @Spoudel347
in Android it worked but in iOS, its not working
s
The
tel:
schema doesn't work on an ios simulator but works on the actual device.
coz simulator doesn't have dialer on ios
s
ho yes
just figured out thanks @Spoudel347 it really helpful 🙏
Copy code
expect class PhoneDialer() {
    fun dialNumber(phoneNumber: Number)
    fun openURL(url: String)

}
Copy code
actual class PhoneDialer actual constructor() : KoinComponent {
    private val context: Context by inject<Context>()
    actual fun dialNumber(phoneNumber: Number) {
        val intent = Intent(Intent.ACTION_DIAL).apply {
            data = Uri.parse("tel:$phoneNumber")
        }
        context.startActivity(intent)
    }

    actual fun openURL(url: String) {

    }

}
Copy code
actual class PhoneDialer actual constructor() {

    actual fun dialNumber(phoneNumber: Number) {
        val phoneURL = NSURL(string = "tel://$phoneNumber")
        if (UIApplication.sharedApplication.canOpenURL(phoneURL)) {
            UIApplication.sharedApplication.openURL(phoneURL)
        } else {
            println("Cannot open dialer")
        }
    }

    actual fun openURL(url: String) {
        val webURL = NSURL(string = url)
        if (UIApplication.sharedApplication.canOpenURL(webURL)) {
            UIApplication.sharedApplication.openURL(webURL)
        } else {
            println("Cannot open URL")
        }
    }
}
I was about to write this
s
No problem, glad I can help.