We are using CNContactStore to fetch contacts from iphone, but we are getting all contacts from my iphone which is giving all synched contacts like facebook and other gmail contacts too, but i need to fetch only the contacts stored in iphone. how to do that,please help me to get out of this.
Asked
Active
Viewed 420 times
0
-
Any one have knowledge about the issue mentioned above – Vetri Oct 22 '15 at 05:43
-
If you look at `[store containersMatchingPredicate:nil error:&error]`, do you get multiple hits (i.e. one for FB, one for gmail, etc.)? If so, you can use `predicateForContactsInContainerWithIdentifier:` to only get contacts for the container of interest. – Rob Oct 22 '15 at 07:01
1 Answers
0
import these
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
- (void) fetchContactsFromDevice {
//1
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil) {
ABAddressBookRequestAccessWithCompletion( addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
}
if (granted) {
// if they gave you permission, then just carry on
[self listPeopleInAddressBook:addressBook];
} else {
// however, if they didn't give you permission, handle it gracefully, for example...
dispatch_async(dispatch_get_main_queue(), ^{
// BTW, this is not on the main thread, so dispatch UI updates back to the main queue
[[[UIAlertView alloc] initWithTitle:nil message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
});
}
CFRelease(addressBook);
});
} else {
NSLog(@"Error reading Address Book : %@", error);
}
}
This method will help you in fetching only addressBook numbers
and then use it to plot
- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook {
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger numberOfPeople = [allPeople count];
for (NSInteger i = 0; i < numberOfPeople; i++) {
ABRecordRef person = (__bridge ABRecordRef)allPeople[i];
NSMutableDictionary * user = [[NSMutableDictionary alloc] init];
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
[user setObject:firstName forKey:@"firstname"];
[user setObject:lastName forKey:@"lastname"];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
[user setObject:phoneNumber forKey:@"phoneNumbers"];
}
CFRelease(phoneNumbers);
[self.contactDataArray addObject:user];
}
[yourtable reloadData];
}
Hope this will help

Appi
- 54
- 5
-
He asked about Contacts framework, not the AddressBook framework (which you shouldn't be using unless you need to support older OS versions). As an aside, this code looks suspiciously like my code from another answer: http://stackoverflow.com/a/23418263/1271826 – Rob Oct 22 '15 at 06:33
-
I am really sorry but this code is from my project implementation but i know this help him. – Appi Oct 22 '15 at 06:43