Saeedeh
Saeedeh

Reputation: 295

open default contacts app in android using react native

I want to open default contacts app in android in react native and try this code, but I dont know the correct url of contacts app in android:

<TouchableOpacity onPress={() => Linking.openURL('ContactsContract.Contacts.CONTENT_URI')}>
      <Text>Press me</Text>
    </TouchableOpacity>

the error is that no activity found o handle intent

Upvotes: 1

Views: 2233

Answers (1)

user3764429
user3764429

Reputation: 402

I don't know, what do you want to achieve in your app, so I've assumed two scenarios:

1. Reading/writing the contacts

It is important to note, that to load the Contacts in your app, you have to request permissions beforehand.

import { PermissionsAndroid } from 'react-native';

PermissionsAndroid.request(
  PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
  {
    'title': 'Contacts',
    'message': 'This app would like to view your contacts.'
  }
).then(() => {
  // open the permissions app
})

You also have to add your permission to AndroidManifest.xml.

<uses-permission android:name="android.permission.READ_CONTACTS" />

You might also need WRITE_CONTACTS permission, it depends on what you want to achieve.

Then, you can use react-native-contacts-wrapper to open the Contacts app.

2. Opening the contacts app only

If you only want to open the contacts app from your app, then you can use the following code:

Linking.openURL('content://com.android.contacts/contacts')

Upvotes: 4

Related Questions