Skip to content

ContactPicker is a component written in Kotlin Multiplatform that natively implements selecting a contact for Android and iOS.

Notifications You must be signed in to change notification settings

dalafiarisamuel/ContactPickerKMP

Repository files navigation

ContactPicker Maven Central

The ContactPicker is a component written in Kotlin Multiplatform that natively implements selecting a contact for Android and iOS.

How to use:

  • In build.gradle file, add this dependency
    commonMain.dependencies {
        implementation("io.github.dalafiarisamuel:contactpicker:0.1.1")
    }
    

Code sample:

  1. Read Contacts Permission

    Android

    On Android you need to add the following permission to your AndroidManifest.xml file:

    <!-- For Read Contacts -->
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    iOS

    On iOS you need to add the following key to your Info.plist file:

    <key>NSContactsUsageDescription</key>
    <string>Contacts permission is required to access user's contacts</string>

    The string value is the message that will be displayed to the user when the permission is requested.

  2. Using rememberContactPickerState() composable

    //in your commonMain package
    @Composable
    fun ContactPickerComponent() {
        
        val contactPicker = rememberContactPickerState {
            println(it)
        }
    
        val contactSelected by contactPicker.value
        
        //handle platform specific contact permission before calling launchContactPicker()
    
        Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
            Button(onClick = { contactPicker.launchContactPicker() }) {
                Text("Pick Contact!")
            }
    
            Spacer(modifier = Modifier.padding(20.dp))
    
            Text("Selected Contact: ${contactSelected?.name}")
    
        }
    }

About

ContactPicker is a component written in Kotlin Multiplatform that natively implements selecting a contact for Android and iOS.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published