Swiftpack.co - yonat/ContactsChangeNotifier as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by yonat.
yonat/ContactsChangeNotifier 1.0.6
Which contacts changed outside your iOS app? Better CNContactStoreDidChange notification: Get real changes, without the noise.
⭐️ 14
πŸ•“ 31 weeks ago
iOS
.package(url: "https://github.com/yonat/ContactsChangeNotifier.git", from: "1.0.6")

ContactsChangeNotifier

Which contacts changed outside your iOS app? Better CNContactStoreDidChange notification: Get real changes, without the noise.

Swift Version License CocoaPods Compatible Platform PRs Welcome

Why Oh Why

Sadly, the Contacts changes API is a mess:

  • The CNContactStoreDidChange notification is received for changes your own code did, not just outside your app. 🀷
  • It contains undocumented userInfo fields. πŸ™ˆ
  • To get the actual changes, you need to use an Objective-C API that is not even callable from Swift. 😱
  • That API is easy to get wrong, and requires maintaining opaque state, or receiving the complete changes history. 🧨

It’s the API that time forgot. πŸ§Ÿβ€β™‚οΈ

ContactsChangeNotifier Features

  • Only get notified for changes outside your app. 🎯
  • Get the list of changes included in the notification. 🎁
  • Only get changes since last notification, not the full all-time history. ✨
  • No Objective-C required. πŸ’₯

Usage

  1. Get the user's Contacts access permission (see docs).
  2. Keep a ContactsChangeNotifier instance - it will observe all Contacts changes but post only those that from outside your app.
  3. Observe ContactsChangeNotifier.didChangeNotification notification.
  4. 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
        }
    }
}

Installation

CocoaPods:

pod 'ContactsChangeNotifier'

Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/yonat/ContactsChangeNotifier", from: "1.0.6")
]

GitHub

link
Stars: 14
Last commit: 31 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics