what’s currently the best way to open a url in a b...
# compose
k
what’s currently the best way to open a url in a browser in a compose multiplatform project?
k
r
wasm:
Copy code
window.location.href = url
android:
Copy code
activity.startActivity(
    Intent(Intent.ACTION_VIEW).apply {
        data = Uri.parse(url)
    })
desktop:
Copy code
try {
    Desktop.getDesktop().browse(URI(url))
} catch (e: Exception) {
    println("Failed to open URL: $url with error: ${e.message}")
}
iOS:
Copy code
val nsURL = NSURL.URLWithString(url)
nsURL?.let {
    UIApplication.sharedApplication.openURL(it)
}
k
thanks! i also have that expect/actual strategy but was trying to minimize and was hoping for an out-of-the-box solution. the generated project seems cleaner so i’ll take inspiration from that!
🎉 1
m
You do it in a easier way with Compose
Copy code
val uriHandler = LocalUriHandler.current
uriHandler.openUri(
    uri = "link",
)
🙌 3
K 1
k
It's called to wrap with another layer of abstraction
I don't see a reason to have the handler class here and provide it via a compose state
k
the handler way does seem easier and still does the job. @Konstantin Tskhovrebov could you tell more about not using the handler class? it seems you are recommending against it but correct me if i misunderstood your statement
k
Oh! My bad) I haven't known about
androidx.compose.ui.platform.LocalUriHandler
! I thought @mohamed rejeb says to implement UriHandler by my own
I think I will update my wizard 👍
🙌 4
done
617 Views