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() { ...@@ -118,34 +118,60 @@ export default function App() {
const allGranted = Object.values(statuses).every(s => s === RESULTS.GRANTED); const allGranted = Object.values(statuses).every(s => s === RESULTS.GRANTED);
// ✅ New: Function to insert contact
const insertContact = async () => { const createMultipleContacts = async () => {
try { try {
// double-check permission // 1️⃣ Ensure permission granted
const perm = Platform.OS === 'android' const readPerm = Platform.OS === 'android'
? await check(PERMISSIONS.ANDROID.WRITE_CONTACTS) ? await check(PERMISSIONS.ANDROID.READ_CONTACTS)
: await check(PERMISSIONS.IOS.CONTACTS); : await check(PERMISSIONS.IOS.CONTACTS);
const writePerm = Platform.OS === 'android'
if (perm !== RESULTS.GRANTED) { ? await check(PERMISSIONS.ANDROID.WRITE_CONTACTS)
Alert.alert("Permission Needed", "Please grant contacts permission to save 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; 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 = { const newContact = {
displayName: 'Support Line', displayName: c.name,
givenName: 'Support', givenName: c.name,
familyName: 'Line', phoneNumbers: [{ label: 'mobile', number: c.number }],
phoneNumbers: [{ label: 'mobile', number: '+60123456789' }],
emailAddresses: [{ label: 'work', email: 'support@example.com' }],
}; };
try {
await Contacts.addContact(newContact); 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) { } catch (error) {
console.error('Failed to add contact:', error); console.error('Error creating contacts:', error);
Alert.alert('❌ Error', String(error)); Alert.alert('Error', String(error));
} }
}; };
const reloadCallDirectory = async () => { const reloadCallDirectory = async () => {
if (Platform.OS !== "ios" || !CallDirectoryManager) return; if (Platform.OS !== "ios" || !CallDirectoryManager) return;
...@@ -180,7 +206,7 @@ export default function App() { ...@@ -180,7 +206,7 @@ export default function App() {
<Button title="Check Again" onPress={checkAllPermissions} /> <Button title="Check Again" onPress={checkAllPermissions} />
<Button title="➕ Add Contact" onPress={insertContact} /> <Button title="➕ Add Contact" onPress={createMultipleContacts} />
{Platform.OS === "ios" && ( {Platform.OS === "ios" && (
<Button title="Reload Call Directory" onPress={reloadCallDirectory} /> <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