29 lines
791 B
TypeScript
29 lines
791 B
TypeScript
import { PermissionsAndroid, Platform, NativeModules } from "react-native";
|
|
|
|
const { ContactsModule } = NativeModules;
|
|
|
|
async function fetchContacts() {
|
|
if (Platform.OS === "android") {
|
|
const granted = await PermissionsAndroid.request(
|
|
PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
|
|
{
|
|
title: "Contacts Permission",
|
|
message: "This app would like to view your contacts.",
|
|
buttonPositive: "OK",
|
|
}
|
|
);
|
|
|
|
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
|
|
console.log("Contacts permission denied");
|
|
return;
|
|
}
|
|
}
|
|
|
|
try {
|
|
const contacts = await ContactsModule.getContacts();
|
|
console.log("Contacts:", contacts);
|
|
} catch (error) {
|
|
console.error("Error fetching contacts:", error);
|
|
}
|
|
}
|
|
export { fetchContacts }; |