The ContactPicker is a component written in Kotlin Multiplatform that natively implements selecting a contact for Android and iOS.
- In build.gradle file, add this dependency
commonMain.dependencies { implementation("io.github.dalafiarisamuel:contactpicker:0.1.1") }
-
Read Contacts Permission
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" />
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.
-
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}") } }