The ContactsManager SDK stores contact data locally on your device, enabling basic offline functionality. This guide explains how contacts are managed in offline scenarios and how synchronization works.
You can fetch contacts from the local database at any time, regardless of connection status:
Copy
do { // Fetch contacts from local storage let contacts = try await ContactsService.shared.fetchContacts() print("Retrieved \(contacts.count) contacts from local storage")} catch { print("Error fetching contacts: \(error.localizedDescription)")}
You can also fetch contacts with specific field types:
Copy
do { // Fetch only contacts with phone numbers let contactsWithPhones = try await ContactsService.shared.fetchContacts( fieldType: .phone ) print("Found \(contactsWithPhones.count) contacts with phone numbers")} catch { print("Error fetching contacts: \(error.localizedDescription)")}
The SDK automatically attempts to synchronize contacts when the app becomes active, when the contact store changes, or when contacts access is granted. You can also trigger manual synchronization:
Copy
do { // Force a sync with the server let syncedCount = try await ContactsService.shared.syncContacts() print("Synchronized \(syncedCount) contacts successfully")} catch { print("Synchronization error: \(error.localizedDescription)")}
To follow a contact (will be synchronized when online):
Copy
do { let result = try await ContactsService.shared.socialService.followContact( followedId: contactId, contactId: contactId ) if let success = result.success, success { print("Follow request processed") } else if let alreadyFollowing = result.alreadyFollowing, alreadyFollowing { print("Already following this contact") }} catch { print("Error processing follow request: \(error.localizedDescription)")}
The ContactsManager SDK uses the OSLog system for logging important events. To view these logs, use the Console app on macOS with your device connected, and filter for the subsystem “com.contactsmanager”.