I am new to iOS and I have fetched contacts in my app, but how to fetch the contact id... suppose if there are two numbers saved with the same name, so how to fetch that particular contact name's id?
Asked
Active
Viewed 1,452 times
0
-
are you using ABAddressbook or Contacts framework, many method of ABAddressbook are deprecated in the iOS 9 ? – HardikDG Apr 26 '16 at 12:46
-
can you tell me the process for fetching contacts and contact ID using Contacts framework...i have used ABaddressbook framework, but its deprecated – Amrut Gaikwad Apr 26 '16 at 13:32
-
are you using swift or objective- c? – HardikDG Apr 26 '16 at 16:14
-
i am using objective c – Amrut Gaikwad Apr 27 '16 at 05:22
1 Answers
2
You may try like this : import framework
#import <Contacts/Contacts.h>
Code
- (void) getContacts {
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES) {
//keys with fetching properties
NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
NSString *containerId = store.defaultContainerIdentifier;
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
NSError *error;
NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@", error);
} else {
for (CNContact *contact in cnContacts) {
//store all the contacts as per your requirement
NSLog(@"Id %@",contact.identifier);//the contact id which you want
NSLog(@"Name %@",contact.givenName);
}
}
}
}];
}

HardikDG
- 5,892
- 2
- 26
- 55
-
@AmrutGaikwad Glad that it helps if this answer helps you to resolve your question please select answer as correct. Thanks – HardikDG Apr 28 '16 at 06:00
-
ok..can you also tell me how to add that fetched contact array to a table view? – Amrut Gaikwad Apr 28 '16 at 08:09
-
provide the array index to cellForRowAtIndexPath method index and get data from particular object via key – HardikDG Apr 28 '16 at 09:41
-
cell.textLabel.text=[[_ContactsCN objectAtIndex:indexPath.row]givenName]; [_table reloadData]; – Amrut Gaikwad Apr 28 '16 at 10:28
-
here above is my code for table view- cellForRowAtIndexPath method,...._ContactCN is my array and givenName is the key....but i am still not able to display the table..plz help me – Amrut Gaikwad Apr 28 '16 at 10:28
-
try cell.textLabel.text = [_ContactsCN[indexPath.row] valueForKey:@"givenName"] also you don't need '[_table reloadData];' inside cellForRow – HardikDG Apr 28 '16 at 17:09
-
thanks for the suggestion, but i am still not able to display data on tableview, i tried every other possibility, but my table view is blank. – Amrut Gaikwad Apr 29 '16 at 06:06
-
-
and i also added the cnContacts array to my _ContactsCN array using add object – Amrut Gaikwad Apr 29 '16 at 06:44