Browser
Code
Open a framework or use some kotlin compose code?
AI
Gemini
Source from this article
another github project
Here is how to open a web browser in Kotlin Multiplatform (KMP):
For opening a web browser in KMP, the expected/actual mechanism is used to handle platform-specific implementations.
• Define the expect function: In the common code, an expect function is declared. This function defines the interface for opening a browser.
expect fun openInBrowser(url: String, completionHandler: (Boolean) -> Unit)
• Implement actual functions: Platform-specific implementations are provided using the actual keyword.
• Android: Uses Intent to open the browser.
import android.content.Intent
import android.net.Uri
import android.content.ActivityNotFoundException
import android.content.Context
actual fun openInBrowser(url: String, completionHandler: (Boolean) -> Unit) {
val result = try {
context.startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
)
true
} catch (_: ActivityNotFoundException) {
false
}
completionHandler(result)
}
• iOS: Uses UIApplication.shared.open to open the browser.
import platform.UIKit.UIApplication
import platform.Foundation.NSURL
actual fun openInBrowser(url: String, completionHandler: (Boolean) -> Unit) {
val nsURL = NSURL.URLWithString(url)
if (nsURL != null) {
UIApplication.sharedApplication.openURL(nsURL)
completionHandler(true)
} else {
completionHandler(false)
}
}
• Desktop: Uses java.awt.Desktop.getDesktop().browse() to open the browser.
import java.awt.Desktop
import java.net.URI
actual fun openInBrowser(url: String, completionHandler: (Boolean) -> Unit) {
val result = try {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(URI(url))
true
} else {
false
}
} catch (e: Exception) {
false
}
completionHandler(result)
}
• Call the function: Call the openInBrowser function from your common code, and the correct platform-specific implementation will be executed.
openInBrowser("https://www.example.com") { success ->
if (success) {
println("Browser opened successfully")
} else {
println("Failed to open browser")
}
}
AI responses may include mistakes.
[-] https://dev.to/tkuenneth/open-the-default-browser-across-platforms-3cbh
Browser specific code?
// Common - WebBrowser.kt
expect fun openWebBrowser(url: String): Boolean
// Android - WebBrowser.android.kt
import android.content.Intent
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent
import com.stevdza_san.webbrowser.AppContext
actual fun openWebBrowser(url: String): Boolean {
val intent = CustomTabsIntent.Builder().build().apply {
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
return try {
val context = AppContext.get()
intent.launchUrl(context, Uri.parse(url))
true
} catch (e: Exception) {
println("$TAG: ${e.message}")
false
}
}
// iOS - WebBrowser.ios.kt
import platform.Foundation.NSURL
import platform.UIKit.UIApplication
actual fun openWebBrowser(url: String): Boolean {
val website = NSURL(string = url)
return if (UIApplication.sharedApplication().canOpenURL(website)) {
runCatching {
var completed = false
UIApplication.sharedApplication()
.openURL(
website,
mapOf<Any?, Any>()
) { success ->
completed = success
}
completed
}.getOrElse {
println("$TAG: Failed to open the URL. ${it.message}")
false
}
} else {
println("$TAG: Couldn't open the URL.")
false
}
}