Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save delacrixmorgan/8b9ba5b95070729edbf4c4ab91a6960f to your computer and use it in GitHub Desktop.
Save delacrixmorgan/8b9ba5b95070729edbf4c4ab91a6960f to your computer and use it in GitHub Desktop.
KMP - Open URL In Browser with Context
actual fun openUrlInBrowser(url: String) {
    val intent = Intent(Intent.ACTION_VIEW).apply {
        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        data = Uri.parse(url)
    }

    AppContext.get().startActivity(intent)
}

actual object AppContext {
    private lateinit var application: Application

    @Synchronized
    fun init(context: Context) {
        check(!::application.isInitialized) { "Application already initialized" }
        application = context.applicationContext as Application
    }

    fun get(): Context {
        if (::application.isInitialized.not()) throw Exception("Application context isn't initialized")
        return application.applicationContext
    }
}

Or you can easily just do this.

val uriHandler = LocalUriHandler.current
uriHandler.openUri("https://github.com/delacrixmorgan")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment