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.
- 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.
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 ContactStoreimport ContactStorelet store = ContactStore.sharedlet status = store.authorizationStatus()
if status != .authorized {
do {
try await store.requestAccess()
} catch {
// Handle access request error
}
}- 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
}- 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)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.
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 onContactStoreto provide a custom closure for creatingCNAuthorizationStatusinstances. - The closure accepts no arguments and returns a
CNAuthorizationStatusvalue.
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 .deniedWe welcome contributions to this project! Please feel free to open issues or pull requests to help improve the package.
LLC Freedom Space – @LLCFreedomSpace – [email protected]
Distributed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3. See LICENSE.md for more information.