A Swift library for Contacts framework.
- iOS 9.0+
To run the example project, clone the repo, and run pod install
from the Example directory first.
ContactManager is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "ContactManager"
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(completionHandler: { (result) in
switch result{
case .Success(response: let contacts):
// Do your thing here with [CNContacts] array
break
case .Error(error: let error):
print(error)
break
}
})
Fetch Contacts on Background Thread
fetchContactsOnBackgroundThread(completionHandler: { (result) in
switch result{
case .Success(response: let contacts):
// Do your thing here with [CNContacts] array
break
case .Error(error: let error):
print(error)
break
}
})
Search Contact
searchContact(SearchString: "john") { (result) in
switch result{
case .Success(response: let contacts):
// Contacts Array includes Search Result Contacts
break
case .Error(error: let error):
print(error)
break
}
}
Get CNContact From Identifire
getContactFromID(Identifire: "XXXXXXXXX", completionHandler: { (result) in
switch result{
case .Success(response: let contact):
// CNContact
break
case .Error(error: 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(response: let bool):
if bool{
print("Contact Sucessfully Added")
}
break
case .Error(error: 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(response: let bool):
if bool{
print("Contact Sucessfully Updated")
}
break
case .Error(error: 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(response: let bool):
if bool{
print("Contact Sucessfully Deleted")
}
break
case .Error(error: let error):
print(error.localizedDescription)
break
}
}
Fetch List Of Groups
fetchGroups { (result) in
switch result{
case .Success(response: let groups):
// List Of Groups in groups array
break
case .Error(error: let error):
print(error.localizedDescription)
break
}
}
Create Group
createGroup(Group_Name: "Satish") { (result) in
switch result{
case .Success(response: let bool):
if bool{
print("Group Sucessfully Created")
}
break
case .Error(error: let error):
print(error.localizedDescription)
break
}
}
Create Group in Container
createGroup(Group_Name: "Satish" , ContainerIdentifire: "ID") { (result) in
switch result{
case .Success(response: let bool):
if bool{
print("Group Sucessfully Created")
}
break
case .Error(error: 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 .Error(error: 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 .Error(error: let error):
print(error.localizedDescription)
break
}
}
Fetch Contacts In Group
fetchContactsInGorup(Group: group) { (result) in
switch result{
case .Success(response: let contacts):
// Do your thing here with [CNContacts] array
break
case .Error(error: let error):
print(error)
break
}
}
// OR Use
fetchContactsInGorup2(Group: group) { (result) in
switch result{
case .Success(response: let contacts):
// Do your thing here with [CNContacts] array
break
case .Error(error: let error):
print(error)
break
}
}
Add Contact To Group
addContactToGroup(Group: group, Contact: contact) { (result) in
switch result{
case .Success(response: let bool):
if bool{
print("Contact Sucessfully Added To Group")
}
break
case .Error(error: let error):
print(error.localizedDescription)
break
}
}
Remove Contact From Group
removeContactFromGroup(Group: group, Contact: contact) { (result) in
switch result{
case .Success(response: let bool):
if bool{
print("Contact Sucessfully Added To Group")
}
break
case .Error(error: let error):
print(error.localizedDescription)
break
}
}
Convert [CNContacts] TO CSV
contactsToVCardConverter(contacts: ContactsArray) { (result) in
switch result {
case .Success(response: data):
// Use file extension will be .vcf
break
case .Error(error: let error):
print(error.localizedDescription)
break
}
}
Convert CSV TO [CNContact]
VCardToContactConverter(data: data) { (result) in
switch result{
case .Success(response: let contacts):
// Use Contacts array as you like
break
case .Error(error: let error):
print(error.localizedDescription)
break
}
}
Find Duplicates Contacts
findDuplicateContacts(Contacts: contactsArray) { (duplicatesContacts) in
//Duplicates Contacts Array
//Array type [Array<CNContact>]
}
Viraj Patel, mspviraj@hotmail.com
ContactManager is available under the MIT license. See the LICENSE file for more info.