Git Product home page Git Product logo

contactschangenotifier's Introduction

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.8")
]

contactschangenotifier's People

Contributors

yonat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

contactschangenotifier's Issues

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.

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.