A Swift library for Contacts framework.
A Swift library for Contacts framework.
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: "[email protected]")
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: "[email protected]") { 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, [email protected]
SwiftyContacts is available under the MIT license. See the LICENSE file for more info.