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

Overview

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.1")
]
You might also like...
The hassle-free way to add Segment analytics to your Swift app (iOS/tvOS/watchOS/macOS/Linux).

Analytics-Swift The hassle-free way to add Segment analytics to your Swift app (iOS/tvOS/watchOS/macOS/Linux/iPadOS). Analytics helps you measure your

A GUI for dynamically creating NSPredicates at runtime to query data in your iOS app.
A GUI for dynamically creating NSPredicates at runtime to query data in your iOS app.

PredicateEditor PredicateEditor is a visual editor for creating and using NSPredicates for querying data in your app. PredicateEditor was inspired by

πŸ” Browse and edit UserDefaults on your app
πŸ” Browse and edit UserDefaults on your app

UserDefaults-Browser Browse and edit UserDefaults on your app. (SwiftUI or UIKit) Browse Edit (as JSON) Edit (Date) Export Note: We recommend to use S

iScheduleYourDay is a watchOS 8.5 app that can help order your daily tasks
iScheduleYourDay is a watchOS 8.5 app that can help order your daily tasks

Currently developing an App for watchOS 8.5 to help order your tasks daily. The app is a simple approach to the actual Apple App Remainders to become an improved version of it

Easy direct access to your database 🎯

OHMySQL β˜…β˜… Every star is appreciated! β˜…β˜… The library supports Objective-C and Swift, iOS and macOS. You can connect to your remote MySQL database usin

Your Data Storage Troubleshooter πŸ› 
Your Data Storage Troubleshooter πŸ› 

Your Data Storage Troubleshooter πŸ›  Introduction StorageKit is a framework which reduces the complexity of managing a persistent layer. You can easily

A Generic CoreData Manager to accept any type of objects. Fastest way for adding a Database to your project.
A Generic CoreData Manager to accept any type of objects. Fastest way for adding a Database to your project.

QuickDB FileManager + CoreData ❗️ Save and Retrieve any thing in JUST ONE line of code ❗️ Fast usage dataBase to avoid struggling with dataBase comple

ObjectBox Swift - persisting your Swift objects superfast and simple
ObjectBox Swift - persisting your Swift objects superfast and simple

ObjectBox Swift ObjectBox is a superfast, light-weight object persistence framework. This Swift API seamlessly persists objects on-device for iOS and

Stuff application – keep track of your stuff.

stuff-app Stuff application – keep track of your stuff. Platforms App to be released for iOS 15, iPadOS 15 and macOS 12 Monterey. Purpose Project is d

Comments
  • Unusable from extensions

    Unusable from extensions

    While the change notification will not be sent to extensions, it would still be beneficial if the code could build when targeting extensions. This allows including it as a dependency with other code that is shared between an app and its extension(s).

    The problematic code is accessing UIApplication.shared in ContactsChangeNotifier.swift:140.

    opened by avi-screenovate 4
  • Phone number can't be fetched

    Phone number can't be fetched

    Hello, the demo is working fine I tried to make in swift by installing pod and swift also works fine but the issue is how can I get the contact number? if anyone have idea about it let me know

    opened by margiPalanBVI 1
Owner
Yonat Sharon
Freelance iOS developer. Agile, TDD, and all that jazz. 🎸
Yonat Sharon
Health Care gives tips to people about his/her life to be better.

Health Care Health Care gives tips to people about his/her life to be better. How does it do this? This application examines and evaluates the health

Mehmet ateş 2 Sep 22, 2022
Server-driven SwiftUI - Maintain iOS apps without making app releases.

ServerDrivenSwiftUI Maintain ios apps without making app releases. Deploy changes to the server and users will receive changes within minutes. This pa

null 9 Dec 29, 2022
Synco - Synco uses Firebase's Realtime Database to synchronize data across multiple devices, in real time

Synco Synco uses Firebase's Realtime Database to synchronize a color across mult

Alessio 0 Feb 7, 2022
Unrealm is an extension on RealmCocoa, which enables Swift native types to be saved in Realm.

Unrealm enables you to easily store Swift native Classes, Structs and Enums into Realm . Stop inheriting from Object! Go for Protocol-Oriented program

Artur  Mkrtchyan 518 Dec 13, 2022
Shows the issue with swift using an ObjC class which has a property from a swift package.

SwiftObjCSwiftTest Shows the issue with swift using an ObjC class which has a property from a swift package. The Swift class (created as @objc derived

Scott Little 0 Nov 8, 2021
SwiftResponsiveLabel - A UILabel subclass which responds to touch on specified patterns

SwiftResponsiveLabel A UILabel subclass which responds to touch on specified patterns. It has the following features: It can detect pattern specified

Susmita Horrow 15 Dec 5, 2022
ResponsiveLabel A UILabel subclass which responds to touch on specified patterns.

#ResponsiveLabel A UILabel subclass which responds to touch on specified patterns. It has the following features: It can detect pattern specified by r

Susmita Horrow 219 Nov 16, 2022
PJAlertView - This library is to make your own custom alert views to match your apps look and feel

PJAlertView - This library is to make your own custom alert views to match your apps look and feel

prajeet 6 Nov 10, 2017
Check your Valorant store from your phone!

Valorant Store Checker Description VSC (Valorant Store Tracker) is an open source iOS app that allows you to track your store and preview your skins.

Gordon 20 Dec 29, 2022
An iOS app to lift the speed limit of your Cowboy Bike

Legal Notice 1. I am not resposible to anything worse that could happen to your bike because of this app 2. Be careful that lifting the 25km/h limit c

null 74 Dec 15, 2022