Commit a23600ed authored by Wei Han's avatar Wei Han

can create multiple contacts at once

parent 7dfd54fa
......@@ -118,34 +118,60 @@ export default function App() {
const allGranted = Object.values(statuses).every(s => s === RESULTS.GRANTED);
// ✅ New: Function to insert contact
const insertContact = async () => {
const createMultipleContacts = async () => {
try {
// double-check permission
const perm = Platform.OS === 'android'
? await check(PERMISSIONS.ANDROID.WRITE_CONTACTS)
// 1️⃣ Ensure permission granted
const readPerm = Platform.OS === 'android'
? await check(PERMISSIONS.ANDROID.READ_CONTACTS)
: await check(PERMISSIONS.IOS.CONTACTS);
if (perm !== RESULTS.GRANTED) {
Alert.alert("Permission Needed", "Please grant contacts permission to save contacts.");
const writePerm = Platform.OS === 'android'
? await check(PERMISSIONS.ANDROID.WRITE_CONTACTS)
: RESULTS.GRANTED;
if (readPerm !== RESULTS.GRANTED || writePerm !== RESULTS.GRANTED) {
const reqRead = Platform.OS === 'android'
? await request(PERMISSIONS.ANDROID.READ_CONTACTS)
: await request(PERMISSIONS.IOS.CONTACTS);
const reqWrite = Platform.OS === 'android'
? await request(PERMISSIONS.ANDROID.WRITE_CONTACTS)
: RESULTS.GRANTED;
if (reqRead !== RESULTS.GRANTED || reqWrite !== RESULTS.GRANTED) {
Alert.alert('Permission Denied', 'Cannot create contacts without permission.');
return;
}
}
// 2️⃣ Define your contact list
const contactsToCreate = [
{ name: 'Support Line', number: '+60123456789' },
{ name: 'Sales Hotline', number: '+60111222333' },
{ name: 'Emergency', number: '+60199887766' },
];
// 3️⃣ Loop & insert contacts
for (const c of contactsToCreate) {
const newContact = {
displayName: 'Support Line',
givenName: 'Support',
familyName: 'Line',
phoneNumbers: [{ label: 'mobile', number: '+60123456789' }],
emailAddresses: [{ label: 'work', email: 'support@example.com' }],
displayName: c.name,
givenName: c.name,
phoneNumbers: [{ label: 'mobile', number: c.number }],
};
try {
await Contacts.addContact(newContact);
Alert.alert('✅ Success', 'Contact added successfully');
console.log(`✅ Contact added: ${c.name}`);
} catch (err) {
console.error(`❌ Failed to add contact ${c.name}:`, err);
}
}
Alert.alert('Done ✅', `Created ${contactsToCreate.length} contacts.`);
} catch (error) {
console.error('Failed to add contact:', error);
Alert.alert('❌ Error', String(error));
console.error('Error creating contacts:', error);
Alert.alert('Error', String(error));
}
};
};
const reloadCallDirectory = async () => {
if (Platform.OS !== "ios" || !CallDirectoryManager) return;
......@@ -180,7 +206,7 @@ export default function App() {
<Button title="Check Again" onPress={checkAllPermissions} />
<Button title="➕ Add Contact" onPress={insertContact} />
<Button title="➕ Add Contact" onPress={createMultipleContacts} />
{Platform.OS === "ios" && (
<Button title="Reload Call Directory" onPress={reloadCallDirectory} />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment