Skip to content

LLCFreedom-Space/fs-contact-store

FSContactStore

Swift Version License GitHub release (with filter) Read the Docs example workflow example workflow example workflow codecov

FSContactStore is a Swift package that provides a convenient and easy-to-use interface for interacting with Apple's Contacts framework on iOS and macOS.

Features

  • Simplified authorization handling for requesting access to contacts.
  • Flexible fetching options for retrieving contacts with filtering, sorting, and specifying properties to retrieve.
  • Support for unified contacts, which combine information from multiple sources.
  • Shared instance for quick access to common functionalities.
  • Customizable save requests to allow tailoring logic for adding, updating, and deleting contacts.

Installation

Add the package to your Package.swift file:

dependencies: [
.package(url: "https://github.com/LLCFreedom-Space/fs-contact-store", from: "1.0.0")
]

Import the package in your Swift files:

import ContactStore

Usage

Import the library

import ContactStore

Accessing the Shared Instance

let store = ContactStore.shared

Checking Authorization Status

let status = store.authorizationStatus()

if status != .authorized {
do {
try await store.requestAccess()
} catch {
// Handle access request error
}
}

Fetching Contacts

  • Fetch all contacts with required keys:
do {
let contacts = try await store.fetch()
// Access contact properties here
} catch {
// Handle fetch error
}
  • Fetch contacts by name:
let name = "John Doe"
do {
let contacts = try store.fetch(by: name)
// ...
} catch {
// Handle fetch error
}

Managing Contacts

  • Adding a new contact:
let newContact = CNMutableContact()
newContact.givenName = "Jane"
newContact.familyName = "Smith"

do {
try store.add(newContact)
} catch {
// Handle add error
}
  • Updating a contact:
// ... modify contact properties
try store.update(contact)
  • Deleting a contact:
// ... select contact
try store.delete(contact)

Customizing Save Requests (Optional)

By default, ContactStore uses a standard CNSaveRequest instance. You can modify the static closure ContactStore.makeCNSaveRequest to inject custom logic or provide different request implementations. Important: To modify the ContactStore.makeCNSaveRequest closure, you must use the use(_:) method.

ContactStore.use { request in
  // Make changes to the request
  // ...
  return request
}

This code updates the ContactStore.makeCNSaveRequest closure to accept a CNSaveRequest instance as an argument, make any necessary changes to it, and then return the updated request.

Customizing Authorization Status (Optional)

By default, ContactStore uses the actual authorization status from the system's CNContactStore. However, you can override this behavior with a custom closure to inject specific authorization statuses for testing or other purposes.

Here's how to customize the authorization status:

Use the use(_:) method:

  • Call the use(_:) method on ContactStore to provide a custom closure for creating CNAuthorizationStatus instances.
  • The closure accepts no arguments and returns a CNAuthorizationStatus value.

Implement the closure logic:

  • Within the closure, specify the desired authorization status to be returned. You can return a fixed value or implement more complex logic based on your needs.
ContactStore.use {
    return .denied // Simulate denied authorization status
}

let currentStatus = ContactStore.authorizationStatus()
// currentStatus will now be .denied

Contributions

We welcome contributions to this project! Please feel free to open issues or pull requests to help improve the package.

Links

LLC Freedom Space – @LLCFreedomSpacesupport@freedomspace.company

Distributed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3. See LICENSE.md for more information.

GitHub

About

FSContactStore is a Swift package that provides a convenient and easy-to-use interface for interacting with Apple's Contacts framework on iOS and macOS.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages