Hi all,
Apple has introduced new Contacts Framework in iOS 9, which replaces the AddressBook framework. Accessing contacts information using AddressBook framework was very tough task for developer. But new Contacts framework of iOS 9 provides very easy methods to access Contact information. Contacts Framework provides contacts not only from database of you iphone or ipad device but also from various sources like iCloud account and then return the associated contacts. We can also search and filter the contacts using this Contacts Framework.
1) Make an instance of CNContactStore
let store = CNContactStore()
2) Add keys which you required in user contact info
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey,CNContactNicknameKey]
In above code , we are fetching user name , family name ,phone number and user nickname.
3) Execute a query to fetch all user names
do{
try store.enumerateContactsWithFetchRequest(CNContactFetchRequest.init(keysToFetch: keysToFetch), usingBlock: { (contact, pointer) -> Void in
print("contact = ","\(contact)")
})
}
catch{
print("something wrong happened")
}
Now run the above code and you can see all contacts info in your console.
0 Comment(s)