A Swift library for Contacts framework.
- SwiftyContacts
- Requirements
- Installation
- Get started
- async-await
- Requests access to the user's contacts
- Request the current authorization status
- Fetch all contacts from device
- Fetch contacts matching a name.
- Fetch contacts matching an email address.
- Fetch contacts matching a phone number.
- To fetch contacts matching contact identifiers.
- To fetch contacts matching group identifier
- find the contacts in the specified container.
- Fetch a contact with a given identifier.
- Add contact to the contact store.
- Update contact to the contact store.
- Delete contact to the contact store.
- Adds a group to the contact store.
- Fetches all groups in the contact store.
- Updates an existing group in the contact store.
- Deletes a group from the contact store.
- Find the contacts that are members in the specified group.
- Add a new member to a group.
- Removes a contact as a member of a group.
- closures
- Requests access to the user's contacts
- Fetch all contacts from device
- Fetch contacts matching a name.
- Fetch contacts matching an email address.
- Fetch contacts matching a phone number.
- Fetch contacts matching contact identifiers.
- Fetch contacts matching group identifier
- Find the contacts in the specified container.
- Fetch a contact with a given identifier.
- Adds the specified contact to the contact store.
- Updates an existing contact in the contact store.
- Deletes a contact from the contact store.
- Fetches all groups matching the specified predicate.
- Adds a group to the contact store.
- Updates an existing group in the contact store.
- Deletes a group from the contact store.
- Find the contacts that are members in the specified group.
- Add a new member to a group.
- Removes a contact as a member of a group.
- async-await
- Author
- License
- iOS 11.0+ / Mac OS X 10.13+ / watchOS 4.0+
- Xcode 13.0+
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
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"))
]
let access = try await requestAccess()
let status = authorizationStatus()
print(status == CNAuthorizationStatus.authorized)
let contacts = try await fetchContacts()
let contacts = try await fetchContacts(matchingName: "Satish Babariya")
let contacts = try await fetchContacts(matchingEmailAddress: "satish.babariya@gmail.com")
let contacts = try await fetchContacts(matching: CNPhoneNumber(stringValue: "+919426678969"))
let contacts = try await fetchContacts(withIdentifiers: ["id1", "id2" ... ])
let contacts = try await fetchContacts(withGroupIdentifier: "")
let contacts = try await fetchContacts(withContainerIdentifier: "")
let contact = try await fetchContact(withIdentifier: "")
let contact = CNMutableContact()
contact.givenName = "Satish"
try addContact(contact)
guard let contact = contact.mutableCopy() as? CNMutableContact else {
return
}
contact.givenName = "Satish"
try updateContact(contact)
guard let contact = contact.mutableCopy() as? CNMutableContact else {
return
}
try deleteContact(contact)
try addGroup("My Group")
let groups = try await fetchGroups()
guard let group = group.mutableCopy() as? CNMutableGroup else {
return
}
try updateGroup(group)
try deleteGroup(group)
let contacts = try fetchContacts(in: "My Group")
try addContact(contact, to: group)
try deleteContact(contact, from: group)
requestAccess { result in
switch result {
case let .success(bool):
print(bool)
case let .failure(error):
print(error.localizedDescription)
}
}
fetchContacts { result in
switch result {
case let .success(contacts):
print(contacts)
case let .failure(error):
print(error.localizedDescription)
}
}
fetchContacts(matchingName: "Satish") { result in
switch result {
case let .success(contacts):
print(contacts)
case let .failure(error):
print(error.localizedDescription)
}
}
fetchContacts(matchingEmailAddress: "satish.babariya@gmail.com") { result in
switch result {
case let .success(contacts):
print(contacts)
case let .failure(error):
print(error.localizedDescription)
}
}
fetchContacts(matching: CNPhoneNumber(stringValue: "+919426678969")) { result in
switch result {
case let .success(contacts):
print(contacts)
case let .failure(error):
print(error.localizedDescription)
}
}
fetchContacts(withIdentifiers: []) { result in
switch result {
case let .success(contacts):
print(contacts)
case let .failure(error):
print(error.localizedDescription)
}
}
fetchContacts(withGroupIdentifier: "") { result in
switch result {
case let .success(contacts):
print(contacts)
case let .failure(error):
print(error.localizedDescription)
}
}
fetchContacts(withContainerIdentifier: "") { result in
switch result {
case let .success(contacts):
print(contacts)
case let .failure(error):
print(error.localizedDescription)
}
}
fetchContact(withIdentifier: "") { result in
switch result {
case let .success(contact):
print(contact)
case let .failure(error):
print(error.localizedDescription)
}
}
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)
}
}
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)
}
}
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)
}
}
fetchGroups() { result in
switch result {
case let .success(groups):
print(groups)
case let .failure(error):
print(error.localizedDescription)
}
}
addGroup("My Group") { result in
switch result {
case let .success(group):
print(group)
case let .failure(error):
print(error.localizedDescription)
}
}
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)
}
}
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)
}
}
fetchContacts(in: "My Group") { result in
switch result {
case let .success(contacts):
print(contacts)
case let .failure(error):
print(error.localizedDescription)
}
}
addContact(contact, to: group) { result in
switch result {
case let .success(contact):
print(contact)
case let .failure(error):
print(error.localizedDescription)
}
}
removeContact(contact, from: group) { result in
switch result {
case let .success(contact):
print(contact)
case let .failure(error):
print(error.localizedDescription)
}
}
Satish Babariya, satish.babariya@gmail.com
SwiftyContacts is available under the MIT license. See the LICENSE file for more info.