react-native-phonebook
Version:
React Native library to launch phonebook/contacts app and return selected contact details
74 lines (68 loc) • 2.41 kB
JavaScript
import { NativeModules, Platform } from 'react-native';
const LINKING_ERROR = `The package 'react-native-phonebook' doesn't seem to be linked. Make sure: \n\n` + Platform.select({
ios: "- You have run 'cd ios && pod install'\n",
default: ''
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo managed workflow\n';
const Phonebook = NativeModules.Phonebook ? NativeModules.Phonebook : new Proxy({}, {
get() {
throw new Error(LINKING_ERROR);
}
});
/**
* Opens the native phonebook/contacts app and allows user to select contacts
* @param options Configuration options for the phonebook picker
* @returns Promise that resolves with the selected contact(s)
*/
export const openPhonebook = (options = {}) => {
return Phonebook.openPhonebook(options);
};
/**
* Simple contact picker that returns a single contact
* @returns Promise that resolves with the selected contact or null if cancelled
*/
export const pickContact = () => {
return Phonebook.openPhonebook({}).then(result => {
if (result.success && result.contacts && result.contacts.length > 0) {
return result.contacts[0];
}
return null;
});
};
/**
* Opens the native phonebook/contacts app with a callback
* @param callback Function to call when contact selection is complete
* @param options Configuration options for the phonebook picker
*/
export const openPhonebookWithCallback = (callback, options = {}) => {
Phonebook.openPhonebookWithCallback(options, callback);
};
/**
* Checks if the phonebook/contacts app is available on the device
* @returns Promise that resolves to true if available, false otherwise
*/
export const isPhonebookAvailable = () => {
return Phonebook.isPhonebookAvailable();
};
/**
* Requests permission to access contacts
* @returns Promise that resolves to true if permission granted, false otherwise
*/
export const requestContactsPermission = () => {
return Phonebook.requestContactsPermission();
};
/**
* Checks if the app has permission to access contacts
* @returns Promise that resolves to true if permission granted, false otherwise
*/
export const hasContactsPermission = () => {
return Phonebook.hasContactsPermission();
};
export default {
openPhonebook,
pickContact,
openPhonebookWithCallback,
isPhonebookAvailable,
requestContactsPermission,
hasContactsPermission
};
//# sourceMappingURL=index.js.map