Navigation Menu

Skip to content

SwiftyContacts/SwiftyContacts

Repository files navigation

SwiftyContacts

Language: Swift 5 Version License Platform Swift Package Manager Carthage compatible CocoaPods compatible RxSwift: Supported Read the Docs

A Swift library for Contacts framework.

Requirements

  • iOS 11.0+ / Mac OS X 10.13+ / watchOS 4.0+
  • Xcode 13.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate SwiftyContacts into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'SwiftyContacts'

Then, run the following command:

$ pod install

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but SwiftyContacts does support its use on supported platforms.

Once you have your Swift package set up, adding SwiftyContacts as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/SwiftyContacts/SwiftyContacts.git", .upToNextMajor(from: "4.0.0"))
]

Get started

async-await

Requests access to the user's contacts

let access = try await requestAccess()

Request the current authorization status

let status = authorizationStatus()
print(status == CNAuthorizationStatus.authorized)

Fetch all contacts from device

let contacts = try await fetchContacts()

Fetch contacts matching a name.

let contacts = try await fetchContacts(matchingName: "Satish Babariya")

Fetch contacts matching an email address.

let contacts = try await fetchContacts(matchingEmailAddress: "satish.babariya@gmail.com")

Fetch contacts matching a phone number.

let contacts = try await fetchContacts(matching: CNPhoneNumber(stringValue: "+919426678969"))

To fetch contacts matching contact identifiers.

let contacts = try await fetchContacts(withIdentifiers: ["id1", "id2" ... ])

To fetch contacts matching group identifier

let contacts = try await fetchContacts(withGroupIdentifier: "")

find the contacts in the specified container.

let contacts = try await fetchContacts(withContainerIdentifier: "")

Fetch a contact with a given identifier.

let contact = try await fetchContact(withIdentifier: "")

Add contact to the contact store.

let contact = CNMutableContact()
contact.givenName = "Satish"
try addContact(contact)

Update contact to the contact store.

guard let contact = contact.mutableCopy() as? CNMutableContact else {
    return
}
contact.givenName = "Satish"
try updateContact(contact)

Delete contact to the contact store.

guard let contact = contact.mutableCopy() as? CNMutableContact else {
    return
}
try deleteContact(contact)

Adds a group to the contact store.

try addGroup("My Group")

Fetches all groups in the contact store.

let groups = try await fetchGroups()

Updates an existing group in the contact store.

guard let group = group.mutableCopy() as? CNMutableGroup else {
    return
}
try updateGroup(group)

Deletes a group from the contact store.

try deleteGroup(group)

Find the contacts that are members in the specified group.

let contacts = try fetchContacts(in: "My Group")

Add a new member to a group.

try addContact(contact, to: group)

Removes a contact as a member of a group.

try deleteContact(contact, from: group)

closures

Requests access to the user's contacts

requestAccess { result in
    switch result {
    case let .success(bool):
        print(bool)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Fetch all contacts from device

fetchContacts { result in
    switch result {
    case let .success(contacts):
        print(contacts)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Fetch contacts matching a name.

fetchContacts(matchingName: "Satish") { result in
    switch result {
    case let .success(contacts):
        print(contacts)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Fetch contacts matching an email address.

fetchContacts(matchingEmailAddress: "satish.babariya@gmail.com") { result in
    switch result {
    case let .success(contacts):
        print(contacts)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Fetch contacts matching a phone number.

fetchContacts(matching: CNPhoneNumber(stringValue: "+919426678969")) { result in
    switch result {
    case let .success(contacts):
        print(contacts)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Fetch contacts matching contact identifiers.

fetchContacts(withIdentifiers: []) { result in
    switch result {
    case let .success(contacts):
        print(contacts)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Fetch contacts matching group identifier

fetchContacts(withGroupIdentifier: "") { result in
    switch result {
    case let .success(contacts):
        print(contacts)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Find the contacts in the specified container.

fetchContacts(withContainerIdentifier: "") { result in
    switch result {
    case let .success(contacts):
        print(contacts)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Fetch a contact with a given identifier.

fetchContact(withIdentifier: "") { result in
    switch result {
    case let .success(contact):
        print(contact)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Adds the specified contact to the contact store.

let contact = CNMutableContact()
contact.givenName = "Satish"
addContact(contact) { result in
    switch result {
    case let .success(contact):
        print(contact)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Updates an existing contact in the contact store.

guard let contact = contact.mutableCopy() as? CNMutableContact else {
    return
}
contact.givenName = "Satish"
updateContact(contact) { result in
    switch result {
    case let .success(contact):
        print(contact)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Deletes a contact from the contact store.

guard let contact = contact.mutableCopy() as? CNMutableContact else {
    return
}
deleteContact(contact) { result in
    switch result {
    case let .success(contact):
        print(contact)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Fetches all groups matching the specified predicate.

fetchGroups() { result in
    switch result {
    case let .success(groups):
        print(groups)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Adds a group to the contact store.

addGroup("My Group") { result in
    switch result {
    case let .success(group):
        print(group)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Updates an existing group in the contact store.

guard let group = group.mutableCopy() as? CNMutableGroup else {
    return
}
updateGroup(group) { result in
    switch result {
    case let .success(group):
        print(group)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Deletes a group from the contact store.

guard let group = group.mutableCopy() as? CNMutableGroup else {
    return
}
deleteGroup(group) { result in
    switch result {
    case let .success(group):
        print(group)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Find the contacts that are members in the specified group.

fetchContacts(in: "My Group") { result in
    switch result {
    case let .success(contacts):
        print(contacts)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Add a new member to a group.

addContact(contact, to: group) { result in
    switch result {
    case let .success(contact):
        print(contact)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Removes a contact as a member of a group.

removeContact(contact, from: group) { result in
    switch result {
    case let .success(contact):
        print(contact)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Author

Satish Babariya, satish.babariya@gmail.com

License

SwiftyContacts is available under the MIT license. See the LICENSE file for more info.