See change events in the notification’s contactsChangeEvents.
// 2. Keep a ContactsChangeNotifier instance
let contactsChangeNotifier = try! ContactsChangeNotifier(
store: myCNContactStore,
fetchRequest: .fetchRequest(additionalContactKeyDescriptors: myCNKeyDescriptors)
)
// 3. Observe ContactsChangeNotifier.didChangeNotification notification
let observation = NotificationCenter.default.addObserver(
forName: ContactsChangeNotifier.didChangeNotification,
object: nil,
queue: nil
) { notification in
// 4. See change events in the notification's contactsChangeEvents
for event in notification.contactsChangeEvents ?? [] {
switch event {
case let addEvent as CNChangeHistoryAddContactEvent:
print(addEvent.contact)
case let updateEvent as CNChangeHistoryUpdateContactEvent:
print(updateEvent.contact)
case let deleteEvent as CNChangeHistoryDeleteContactEvent:
print(deleteEvent.contactIdentifier)
default:
// group event
break
}
}
}
ContactsChangeNotifier
Which contacts changed outside your iOS app? Better
CNContactStoreDidChange
notification: Get real changes, without the noise.Why Oh Why
Sadly, the Contacts changes API is a mess:
CNContactStoreDidChange
notification is received for changes your own code did, not just outside your app. 🤷userInfo
fields. 🙈It’s the API that time forgot. 🧟♂️
ContactsChangeNotifier Features
Usage
ContactsChangeNotifier
instance - it will observe all Contacts changes but post only those that from outside your app.ContactsChangeNotifier.didChangeNotification
notification.contactsChangeEvents
.Installation
CocoaPods:
Swift Package Manager: