This document is deprecated new document is on the way
A Swift library for Contacts framework.
- iOS 9.0+ / Mac OS X 10.12+ / watchOS 3.0+
- Xcode 9.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
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'SwiftyContacts'
#or
pod 'SwiftyContacts/RxSwift'
For swift 3.x use
// Swift 3.x
pod 'SwiftyContacts' , '~> 2.0.7'
Then, run the following command:
$ pod install
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate SwiftyContacts into your Xcode project using Carthage, specify it in your Cartfile
:
github "SwiftyContacts/SwiftyContacts" ~> 3.0.8
To use SwiftyContacts as a Swift Package Manager package just add the following in your Package.swift file.
import PackageDescription
let package = Package(
name: "HelloSwiftyContacts",
dependencies: [
.Package(url: "https://github.com/satishbabariya/SwiftyContacts", "3.0.8")
]
)
If you prefer not to use either of the aforementioned dependency managers, you can integrate SwiftyContacts into your project manually.
- Download the latest release from https://github.com/satishbabariya/SwiftyContacts/releases
- Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
- In the tab bar at the top of that window, open the "General" panel.
- Click on the
+
button under the "Embedded Binaries" section. - Add the downloaded
SwiftyContacts.framework
. - And that's it!
Import SwiftyContacts into your porject
import SwiftyContacts
For requesting an access for getting contacts. The user will only be prompted the first time access is requested.
requestAccess { (responce) in
if responce {
print("Contacts Acess Granted")
} else {
print("Contacts Acess Denied")
}
}
Determine Status of Acess Permission
authorizationStatus { (status) in
switch status {
case .authorized:
print("authorized")
break
case .denied:
print("denied")
break
default:
break
}
}
Fetch Contacts -- Result will be Array of CNContacts
fetchContacts { (result) in
switch result {
case .success(let contacts):
// Do your thing here with [CNContacts] array
break
case .failure(let error):
break
}
}
Fetch Contacts by Order -- Sorted by CNContactSortOrder -- Result will be Array of CNContacts
fetchContacts(ContactsSortorder: .givenName) { (result) in
switch result{
case .success(let contacts):
// Do your thing here with [CNContacts] array
break
case .failure(let error):
print(error)
break
}
})
Fetch Contacts on Background Thread
fetchContactsOnBackgroundThread(completionHandler: { (result) in
switch result{
case .success(let contacts):
// Do your thing here with [CNContacts] array
break
case .failure(let error):
print(error)
break
}
})
Search Contact
searchContact(SearchString: "john") { (result) in
switch result{
case .success(let contacts):
// Contacts Array includes Search Result Contacts
break
case .failure(let error):
print(error)
break
}
}
Get CNContact From Identifire
getContactFromID(Identifire: "XXXXXXXXX", completionHandler: { (result) in
switch result{
case .success(let contact):
// CNContact
break
case .failure(let error):
print(error)
break
}
})
Add Contact
let contact : CNMutableContact = CNMutableContact()
contact.givenName = "Satish"
// OR Use contact.mutableCopy() For Any CNContact
addContact(Contact: contact) { (result) in
switch result{
case .success(let bool):
if bool{
print("Contact Sucessfully Added")
}
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Add Contact in Container
addContactInContainer(Contact: CNMutableContact, Container_Identifier: String) { (result) in
//Same As Add Contact
}
Update Contact
updateContact(Contact: contact) { (result) in
switch result{
case .success(let bool):
if bool{
print("Contact Sucessfully Updated")
}
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Delete Contact
// Use contact.mutableCopy() To convert CNContact to CNMutableContact
deleteContact(Contact: contact) { (result) in
switch result{
case .success(let bool):
if bool{
print("Contact Sucessfully Deleted")
}
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Fetch List Of Groups
fetchGroups { (result) in
switch result{
case .success(let groups):
// List Of Groups in groups array
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Create Group
createGroup(Group_Name: "Satish") { (result) in
switch result{
case .success(let bool):
if bool{
print("Group Sucessfully Created")
}
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Create Group in Container
createGroup(Group_Name: "Satish" , ContainerIdentifire: "ID") { (result) in
switch result{
case .success(let bool):
if bool{
print("Group Sucessfully Created")
}
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Update Group
updateGroup(Group: group, New_Group_Name: "New Name") { (result) in
switch result{
case .success(response: let bool):
if bool{
print("Group Sucessfully Updated")
}
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Remove Group
removeGroup(Group: group) { (result) in
switch result{
case .success(response: let bool):
if bool{
print("Group Sucessfully Removed")
}
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Fetch Contacts In Group
fetchContactsInGorup(Group: group) { (result) in
switch result{
case .success(let contacts):
// Do your thing here with [CNContacts] array
break
case .failure(let error):
print(error)
break
}
}
// OR Use
fetchContactsInGorup2(Group: group) { (result) in
switch result{
case .success(let contacts):
// Do your thing here with [CNContacts] array
break
case .failure(let error):
print(error)
break
}
}
Add Contact To Group
addContactToGroup(Group: group, Contact: contact) { (result) in
switch result{
case .success(let bool):
if bool{
print("Contact Sucessfully Added To Group")
}
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Remove Contact From Group
removeContactFromGroup(Group: group, Contact: contact) { (result) in
switch result{
case .success(let bool):
if bool{
print("Contact Sucessfully Added To Group")
}
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Convert [CNContacts] TO CSV
contactsToVCardConverter(contacts: ContactsArray) { (result) in
switch result {
case .success(let data):
// Use file extension will be .vcf
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Convert CSV TO [CNContact]
VCardToContactConverter(data: data) { (result) in
switch result{
case .success(let contacts):
// Use Contacts array as you like
break
case .failure(let error):
print(error.localizedDescription)
break
}
}
Find Duplicates Contacts
findDuplicateContacts(Contacts: contactsArray) { (duplicatesContacts) in
//Duplicates Contacts Array
//Array type [Array<CNContact>]
}
rx_requestAccess().subscribe { (event) in
switch event{
case .next(let element):
print(element)
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
print("completed")
break
}
}
rx_fetchContacts().subscribe { (event) in
switch event{
case .next(let contacts):
print(contacts.count)
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
print("completed")
break
}
}
Fetch Contacts By CNContactSortOrder
rx_fetchContacts(ContactsSortorder: .givenName).subscribe { (event) in
switch event{
case .next(let contacts):
print(contacts.count)
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
print("completed")
break
}
}
rx_searchContact(SearchString: "Satish").subscribe { (event) in
switch event{
case .next(let contacts):
print(contacts.count)
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
print("completed")
break
}
}
Search Contacts with Array of Identifiers
rx_ContactsFromIDs(SIdentifires: array).subscribe { (event) in
switch event{
case .next(let contacts):
print(contacts.count)
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
print("completed")
break
}
}
Add new Contact.
rx_addContact(Contact: mutableContact).subscribe { (event) in
switch event{
case .error(let error):
print(error.localizedDescription)
break
case .completed,.next:
//Do your thing
break
}
}
Adds the specified contact to the contact store.
rx_addContactInContainer(Contact: mutableContact, Container_Identifier: idString).subscribe { (event) in
switch event{
case .error(let error):
print(error.localizedDescription)
break
case .completed,.next:
//Do your thing
break
}
}
Updates an existing contact in the contact store.
rx_updateContact(Contact: mutableContact).subscribe { event in
switch event {
case .error(let error):
print(error.localizedDescription)
break
case .completed, .next:
// Do your thing
break
}
}
Deletes a contact from the contact store.
rx_deleteContact(Contact: mutableContact).subscribe { event in
switch event {
case .error(let error):
print(error.localizedDescription)
break
case .completed, .next:
// Do your thing
break
}
}
fetch list of Groups from the contact store.
rx_fetchGroups().subscribe { (event) in
switch event{
case .next(let groups):
//Array of groups
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
break
}
}
Adds a group to the contact store.
rx_createGroup(Group_Name: "Work").subscribe { event in
switch event {
case .error(let error):
print(error.localizedDescription)
break
case .completed, .next:
// Do your thing
break
}
}
Adds a group to the contact store.
rx_createGroupInContainer(Group_Name: "Work", ContainerIdentifire: containerID).subscribe { event in
switch event {
case .error(let error):
print(error.localizedDescription)
break
case .completed, .next:
// Do your thing
break
}
}
Remove an existing group in the contact store.
rx_removeGroup(Group: group).subscribe { event in
switch event {
case .error(let error):
print(error.localizedDescription)
break
case .completed, .next:
// Do your thing
break
}
}
Update an existing group in the contact store.
rx_updateGroup(Group: group).subscribe { event in
switch event {
case .error(let error):
print(error.localizedDescription)
break
case .completed, .next:
// Do your thing
break
}
}
Adds a contact as a member of a group.
rx_addContactToGroup(Group: group, Contact: contact).subscribe { event in
switch event {
case .error(let error):
print(error.localizedDescription)
break
case .completed, .next:
// Do your thing
break
}
}
Remove a contact as a member of a group.
rx_removeContactFromGroup(Group: group, Contact: contact).subscribe { event in
switch event {
case .error(let error):
print(error.localizedDescription)
break
case .completed, .next:
// Do your thing
break
}
}
Fetch all contacts in a group.
rx_fetchContactsInGorup(Group: group).subscribe { (event) in
switch event{
case .next(let contacts):
print(contacts.count)
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
print("completed")
break
}
}
Convert [CNContacts] TO CSV
rx_contactsToVCardConverter(contacts: arrContacts).subscribe { (event) in
switch event{
case .next(let data):
//Do anything.
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
break
}
}
Convert CSV TO [CNContact]
x_VCardToContactConverter(data: data).subscribe { (event) in
switch event{
case .next(let contacts):
print(contacts.count)
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
print("completed")
break
}
}
Convert Array of CNContacts to Data using NSKeyedArchiver
rx_archiveContacts(contacts: arrContacts).subscribe { (event) in
switch event{
case .next(let data):
//Do anything.
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
break
}
}
Convert Data to Array of CNContacts using NSKeyedArchiver
rx_unarchiveConverter(data: data).subscribe { (event) in
switch event{
case .next(let contacts):
print(contacts.count)
break
case .error(let error):
print(error.localizedDescription)
break
case .completed:
print("completed")
break
}
}
Convert CNPhoneNumber To digits
rx_CNPhoneNumberToString(CNPhoneNumber: contact).subscribe { event in
switch event {
case .error(let error):
print(error.localizedDescription)
break
case .completed, .next:
// Do your thing
break
}
}
Making a call.
makeCall(CNPhoneNumber: number)
Satish Babariya, [email protected]
SwiftyContacts is available under the MIT license. See the LICENSE file for more info.