Memulai dengan Contacts
Panduan ini akan memandu Anda mengintegrasikan plugin Capacitor Contacts ke dalam aplikasi Anda.
Instalasi
Section titled “Instalasi”Instal plugin menggunakan npm:
npm install @capgo/capacitor-contactsnpx cap syncKonfigurasi iOS
Section titled “Konfigurasi iOS”Tambahkan berikut ini ke Info.plist Anda:
<key>NSContactsUsageDescription</key><string>This app needs access to contacts to let you select recipients</string>Konfigurasi Android
Section titled “Konfigurasi Android”Tambahkan izin berikut ke AndroidManifest.xml Anda:
<uses-permission android:name="android.permission.READ_CONTACTS" /><uses-permission android:name="android.permission.WRITE_CONTACTS" />Penggunaan Dasar
Section titled “Penggunaan Dasar”Impor Plugin
Section titled “Impor Plugin”import { Contacts } from '@capgo/capacitor-contacts';Minta Izin
Section titled “Minta Izin”const requestPermissions = async () => { const permission = await Contacts.requestPermissions(); console.log('Permission status:', permission.contacts);};Dapatkan Semua Kontak
Section titled “Dapatkan Semua Kontak”const getAllContacts = async () => { const result = await Contacts.getContacts({ projection: { name: true, phones: true, emails: true, image: true } });
console.log('Contacts:', result.contacts);};Cari Kontak
Section titled “Cari Kontak”const searchContacts = async (query: string) => { const result = await Contacts.getContacts({ projection: { name: true, phones: true, emails: true }, query: query });
console.log('Search results:', result.contacts);};Buat Kontak
Section titled “Buat Kontak”const createContact = async () => { const newContact = { name: { given: 'John', family: 'Doe' }, phones: [{ type: 'mobile', number: '+1234567890' }], emails: [{ type: 'work', address: 'john.doe@example.com' }] };
const result = await Contacts.createContact(newContact); console.log('Contact created:', result);};Perbarui Kontak
Section titled “Perbarui Kontak”const updateContact = async (contactId: string) => { const updates = { contactId: contactId, name: { given: 'Jane', family: 'Doe' } };
await Contacts.updateContact(updates); console.log('Contact updated');};Hapus Kontak
Section titled “Hapus Kontak”const deleteContact = async (contactId: string) => { await Contacts.deleteContact({ contactId }); console.log('Contact deleted');};Contoh Lengkap
Section titled “Contoh Lengkap”Berikut adalah contoh lengkap dengan service kontak:
import { Contacts } from '@capgo/capacitor-contacts';
interface Contact { contactId: string; name: { display?: string; given?: string; family?: string; }; phones?: Array<{ type: string; number: string }>; emails?: Array<{ type: string; address: string }>; image?: { base64String: string };}
class ContactsService { async checkPermissions(): Promise<boolean> { const permission = await Contacts.checkPermissions();
if (permission.contacts === 'granted') { return true; }
const requested = await Contacts.requestPermissions(); return requested.contacts === 'granted'; }
async getAllContacts(): Promise<Contact[]> { const hasPermission = await this.checkPermissions(); if (!hasPermission) { throw new Error('Contacts permission denied'); }
const result = await Contacts.getContacts({ projection: { name: true, phones: true, emails: true, image: true } });
return result.contacts; }
async searchContacts(query: string): Promise<Contact[]> { if (!query || query.length < 2) { return []; }
const result = await Contacts.getContacts({ projection: { name: true, phones: true, emails: true }, query: query });
return result.contacts; }
async getContactById(contactId: string): Promise<Contact | null> { const result = await Contacts.getContacts({ projection: { name: true, phones: true, emails: true, image: true, organization: true, birthday: true, note: true, urls: true, postalAddresses: true }, contactId: contactId });
return result.contacts.length > 0 ? result.contacts[0] : null; }
async createContact(contact: Partial<Contact>): Promise<string> { const hasPermission = await this.checkPermissions(); if (!hasPermission) { throw new Error('Contacts permission denied'); }
const result = await Contacts.createContact(contact); return result.contactId; }
async updateContact(contactId: string, updates: Partial<Contact>): Promise<void> { await Contacts.updateContact({ contactId, ...updates }); }
async deleteContact(contactId: string): Promise<void> { await Contacts.deleteContact({ contactId }); }
formatPhoneNumber(phone: string): string { // Remove non-numeric characters const cleaned = phone.replace(/\D/g, '');
// Format as (XXX) XXX-XXXX if (cleaned.length === 10) { return `(${cleaned.slice(0, 3)}) ${cleaned.slice(3, 6)}-${cleaned.slice(6)}`; }
return phone; }
getContactInitials(contact: Contact): string { const given = contact.name?.given || ''; const family = contact.name?.family || '';
if (given && family) { return `${given[0]}${family[0]}`.toUpperCase(); }
const display = contact.name?.display || ''; const parts = display.split(' ');
if (parts.length >= 2) { return `${parts[0][0]}${parts[1][0]}`.toUpperCase(); }
return display.slice(0, 2).toUpperCase(); }}
// Usageconst contactsService = new ContactsService();
// Get all contactsconst contacts = await contactsService.getAllContacts();console.log('Contacts:', contacts);
// Search contactsconst results = await contactsService.searchContacts('john');console.log('Search results:', results);
// Create contactconst newContactId = await contactsService.createContact({ name: { given: 'Jane', family: 'Smith' }, phones: [{ type: 'mobile', number: '+1234567890' }]});
// Update contactawait contactsService.updateContact(newContactId, { emails: [{ type: 'work', address: 'jane@example.com' }]});Memahami Projection
Section titled “Memahami Projection”Parameter projection mengontrol field mana yang akan diambil:
const result = await Contacts.getContacts({ projection: { name: true, // Contact name phones: true, // Phone numbers emails: true, // Email addresses image: true, // Contact photo organization: true, // Company/organization birthday: true, // Birth date note: true, // Notes urls: true, // Websites postalAddresses: true // Physical addresses }});Tips: Hanya minta field yang Anda butuhkan untuk meningkatkan performa.
UI Pemilih Kontak
Section titled “UI Pemilih Kontak”Banyak aplikasi lebih suka menggunakan pemilih kontak native:
const pickContact = async () => { try { const result = await Contacts.pickContact({ projection: { name: true, phones: true, emails: true } });
if (result.contacts.length > 0) { const contact = result.contacts[0]; console.log('Selected contact:', contact); } } catch (error) { console.error('Contact picker cancelled or failed:', error); }};Praktik Terbaik
Section titled “Praktik Terbaik”- Minta Izin Minimal: Hanya minta izin kontak saat diperlukan
- Gunakan Projection: Hanya ambil field yang benar-benar Anda gunakan
- Tangani Penolakan: Berikan fallback ketika izin ditolak
- Cache dengan Bijak: Kontak bisa berubah, jangan cache terlalu lama
- Hormati Privasi: Bersikap transparan tentang penggunaan kontak
- Operasi Async: Semua operasi kontak bersifat async
Masalah Umum
Section titled “Masalah Umum”Izin Ditolak
Section titled “Izin Ditolak”const handlePermissionDenied = async () => { const permission = await Contacts.checkPermissions();
if (permission.contacts === 'denied') { // Show dialog explaining why permission is needed showPermissionDialog(); // Direct user to settings // On iOS: Settings > [App] > Contacts // On Android: Settings > Apps > [App] > Permissions }};Daftar Kontak Besar
Section titled “Daftar Kontak Besar”const loadContactsInBatches = async () => { // Get count first (lightweight) const projection = { name: true }; // Minimal projection
// Implement pagination if needed const allContacts = await Contacts.getContacts({ projection });
// Process in chunks const chunkSize = 100; for (let i = 0; i < allContacts.contacts.length; i += chunkSize) { const chunk = allContacts.contacts.slice(i, i + chunkSize); await processContactChunk(chunk); }};Langkah Selanjutnya
Section titled “Langkah Selanjutnya”- Jelajahi Referensi API untuk dokumentasi metode lengkap
- Lihat aplikasi contoh untuk penggunaan lanjutan
- Lihat tutorial untuk contoh implementasi lengkap