Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yonat/ContactsChangeNotifier
Which contacts changed outside your iOS app? Better CNContactStoreDidChange notification: Get real changes, without the noise.
https://github.com/yonat/ContactsChangeNotifier
cncontact cncontactstore contacts ios swift
Last synced: 1 day ago
JSON representation
Which contacts changed outside your iOS app? Better CNContactStoreDidChange notification: Get real changes, without the noise.
- Host: GitHub
- URL: https://github.com/yonat/ContactsChangeNotifier
- Owner: yonat
- License: mit
- Created: 2022-07-14T13:41:39.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-20T19:18:19.000Z (14 days ago)
- Last Synced: 2024-10-22T03:28:26.764Z (13 days ago)
- Topics: cncontact, cncontactstore, contacts, ios, swift
- Language: Swift
- Homepage:
- Size: 155 KB
- Stars: 18
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
Awesome Lists containing this project
- fucking-awesome-swift - ContactsChangeNotifier - Which contacts changed outside your app? Better CNContactStoreDidChange notification: Get real changes, without the noise. (Libs / Kit)
- awesome-swift - ContactsChangeNotifier - Which contacts changed outside your app? Better CNContactStoreDidChange notification: Get real changes, without the noise. (Libs / Kit)
README
# ContactsChangeNotifier
Which contacts changed outside your iOS app? Better `CNContactStoreDidChange` notification: Get real changes, without the noise.
[![Swift Version][swift-image]][swift-url]
[![License][license-image]][license-url]
[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/ContactsChangeNotifier.svg)](https://img.shields.io/cocoapods/v/ContactsChangeNotifier.svg)
[![Platform](https://img.shields.io/cocoapods/p/ContactsChangeNotifier.svg?style=flat)](http://cocoapods.org/pods/ContactsChangeNotifier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)## 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](https://developer.apple.com/documentation/contacts/requesting_authorization_to_access_contacts)).
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`.```swift
// 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:
```ruby
pod 'ContactsChangeNotifier'
```### Swift Package Manager:
```swift
dependencies: [
.package(url: "https://github.com/yonat/ContactsChangeNotifier", from: "1.2.0")
]
```[swift-image]:https://img.shields.io/badge/swift-5.0-orange.svg
[swift-url]: https://swift.org/
[license-image]: https://img.shields.io/badge/License-MIT-blue.svg
[license-url]: LICENSE.txt