Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expo plugin #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ For React Native => 0.59 only:
react-native link react-native-select-contact
```

### Expo Usage
since this package use NativeModules you need to
migrate to development build. more info [here](https://docs.expo.dev/develop/development-builds/create-a-build/)

add `react-native-select-contact` to the plugin array in your `app.json`

```json
{
"expo": {
...
"plugins": [
...
"react-native-select-contact"
]
}
}
```

#### Android
For Android support, make sure your manifest file includes permission to read contacts along with a query intent for Android 11+:
```xml
Expand Down
3 changes: 1 addition & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
if (project == rootProject) {
repositories {
google()
mavencentral()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
Expand Down Expand Up @@ -43,7 +43,6 @@ repositories {
url "$rootDir/../node_modules/jsc-android/dist"
}
google()
mavencentral()
}

dependencies {
Expand Down
63 changes: 63 additions & 0 deletions app.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const {
withAndroidManifest,
withInfoPlist,
withPlugins,
} = require("@expo/config-plugins");

const CONTACT_USAGE = "Allow $(PRODUCT_NAME) to access your contacts";

const withIosConfig = (config, props) => {
let permissionString = undefined;
if (props) {
const { contactPermission } = props;
permissionString = contactPermission;
}

return withInfoPlist(config, (config) => {
config.modResults.NSContactsUsageDescription =
permissionString ?? CONTACT_USAGE;

return config;
});
};

const withAndroidConfig = (config) => {
return withAndroidManifest(config, async (config) => {
const androidManifest = config.modResults.manifest;

if (
!config.android.permissions.find(
(permission) =>
permission === "android.permission.READ_CONTACTS"
)
) {
config.android.permissions.push("android.permission.READ_CONTACTS");
}

androidManifest.application[0]?.activity?.[0]?.["intent-filter"].push({
action: {
$: {
"android:name": "android.intent.action.PICK",
},
},
data: {
$: {
"android:mimeType": "vnd.android.cursor.dir/contact",
},
},
category: {
$: {
"android:name": "android.intent.category.DEFAULT",
},
},
});

return config;
});
};

const withSelectContact = (config) => {
return withPlugins(config, [withAndroidConfig, withIosConfig]);
};

module.exports = withSelectContact;
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ const SelectContactApi = {
return null;
}

return selectPhone(phones, textOptions)
const uniquePhones = [...new Map(phones.map(phone => [phone.number.replace(/ /g,''), phone])).values()]

return selectPhone(uniquePhones, textOptions)
.then(selectedPhone => {
return selectedPhone ? { contact, selectedPhone } : null;
});
Expand Down