Skip to content

yjyoon-dev/KInAppBrowser

Repository files navigation

KInAppBrowser

🌐 A lightweight in-app browser library for Kotlin Multiplatform.

✨ Features

  • 🌐 Native In-App Browsing: Chrome Custom Tabs for Android, SFSafariViewController for iOS
  • 📱 Kotlin Multiplatform: Single API for both Android and iOS
  • 🎯 Simple API: Just one method call to open URLs
  • 🔒 Secure: Leverages platform security features and shared authentication
  • Performance: Fast loading with pre-warming and optimized rendering
  • 🎨 Native UI: Platform-consistent user experience

🚀 Installation

Add the dependency to your build.gradle.kts of module:

commonMain.dependencies {
    implementation("dev.yjyoon:kinappbrowser:1.0.0")
}

📖 Usage

Android

Initialize KInAppBrowser in your Activity and open URLs:

import dev.yjyoon.kinappbrowser.KInAppBrowser

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // Initialize KInAppBrowser with current activity
        KInAppBrowser.init(this)
        
        // Open URL in Chrome Custom Tabs
        KInAppBrowser.open("https://github.com/yjyoon-dev/KInAppBrowser")
    }
}

iOS

No initialization needed - directly open URLs:

import dev.yjyoon.kinappbrowser.KInAppBrowser

// Open URL in SFSafariViewController
KInAppBrowser.open("https://github.com/yjyoon-dev/KInAppBrowser")

// Programmatically close Safari view controller when needed
KInAppBrowser.close()

Compose Multiplatform

Works seamlessly with Compose Multiplatform:

@Composable
fun MyScreen() {
    Button(
        onClick = {
            KInAppBrowser.open("https://example.com")
        }
    ) {
        Text("Open Website")
    }
}

🔧 Platform Details

Android Implementation

  • Uses Chrome Custom Tabs for optimal performance and user experience
  • Automatic fallback to default browser if Custom Tabs unavailable
  • Shared cookies and authentication with Chrome
  • Fast loading with pre-warming capabilities
  • Requires Activity context initialization

iOS Implementation

  • Uses SFSafariViewController for full Safari browsing experience
  • Shared cookies and website data with Safari
  • Built-in security features and privacy protection
  • Support for Safari features like Reader mode and AutoFill
  • Automatic handling of different URL schemes like Universal links

📋 Requirements

  • Android: API level 24+ (Android 7.0)
  • iOS: iOS 14.0+

🛠️ API Reference

Common API

expect object KInAppBrowser {
    /**
     * Opens the specified URL in the platform's native in-app browser.
     * 
     * @param url The URL to open
     * @throws IllegalStateException on Android if not initialized
     */
    fun open(url: String)
}

Android-Specific API

/**
 * Initializes KInAppBrowser with the provided Activity.
 * Must be called before using open() on Android.
 * 
 * @param activity The Activity from which Custom Tabs will be launched
 */
fun init(activity: Activity)

iOS-Specific API

/**
 * Closes the currently presented SFSafariViewController.
 * Optional method for programmatic dismissal.
 */
fun close()

📱 Sample App

Check out the sample app for a complete working example with Compose Multiplatform.

📄 License

Copyright 2025 yjyoon

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

🔗 Links

About

A lightweight in-app browser library for Kotlin Multiplatform

Topics

Resources

License

Stars

Watchers

Forks

Languages