https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

Suresh Maidaragi

12/07/2023, 9:03 AM
Hi there how to make phone number and url intent handler in KMM
j

Jan Holešovský

12/07/2023, 10:07 AM
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

Suresh Maidaragi

12/07/2023, 10:10 AM
When user clicks on number we should open dialer Also when clicks on URL we need to open in default phone browser
j

Jan Holešovský

12/07/2023, 10:22 AM
But what do you use for this UI that it is cross-platform?
s

Suresh Maidaragi

12/07/2023, 10:23 AM
yes compose
j

Jan Holešovský

12/07/2023, 10:23 AM
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

Suresh Maidaragi

12/07/2023, 10:27 AM
ho I see
yes platform specific I have to write...
sorry no commercial for now
j

Jan Holešovský

12/07/2023, 10:28 AM
No problem 🙂
s

Spoudel347

12/07/2023, 12:44 PM
If you just need a url handler you can do this.
Copy code
val uriHandler = LocalUriHandler.current

  LaunchedEffect(Unit) {
    uriHandler.openUri("tel:9999999999")
  }
s

Suresh Maidaragi

12/07/2023, 12:48 PM
Trying now @Spoudel347
in Android it worked but in iOS, its not working
s

Spoudel347

12/07/2023, 1:00 PM
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

Suresh Maidaragi

12/07/2023, 1:00 PM
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

Spoudel347

12/07/2023, 1:03 PM
No problem, glad I can help.