Different count in Contacts app and ABAddressBookGetPersonCount - iphone

When I scroll My Contacts App to the bottom, where it shows the number of contacts, It says 2568 contacts.
But in my app when I try to fetch the number of contacts from AddressBook, by ABAddressBookGetPersonCount, I get 2582 contacts.
Has Anyone noticed this? Why is this difference there? Does the AddressBook framework give me wrong information??
EDIT
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSLog(#"num people to send: %ld", nPeople);
NSLog(#"num people in array: %ld", CFArrayGetCount(allPeople));
These two logs, give me the same number, however this number is different from that written at the bottom of the contacts app in iphone.

The contacts app pulls in its contacts from various groups. You can change the number of people in your list by going to the groups select page (top left in IOS 7) and selecting or deselecting your contacts.
If your list is pulling in all the contacts 'available' to you, the difference is due to contacts coming from a source not used in the Contacts App. You may have noticed that you can start to type in an email and it will auto fill the email even though you have no contact for that person. That information is stored inside the AddressBook database, but is not pulled into the Contact App. When you use the ABAddressBookCopyArrayOfAllPeople, it also pulls in these 'contacts'. You can verify this by putting all the contact's in a tableview and printing out the name, first email, first phone number, for each of the contacts. You will probably find a few contacts which only contain an email address (or possibly phone number).
An example of how to print off all the contacts can be found here.

Use this code
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = (ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName));
//CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
CFIndex nPeople = CFArrayGetCount(allPeople); // bugfix who synced contacts with facebook

Related

how to add facebook's friends into iphone's address book

I'm new in iPhone development. I want to add facebook's friends name into iPhone's address book.
I can get get facebook's friend list from the facebook using facebook's sdk.framework.
Can someone help me to insert this friends name into iPhone address book?
You need to read Adress book programing from here
And for inserting contact: idea is -
You need make object of address book ABAddressBookRef addressBook = ABAddressBookCreate();
then create a record like this ABRecordRef person = ABPersonCreate();
then set some property like this
ABRecordSetValue(person, kABPersonFirstNameProperty, CFSTR(fbUserName), &anError);
fbUserName is the name of your friend getting by fbGraph api.
then you need to insert this record in address book ABAddressBookAddRecord();
then save the changes. ABAddressBookSave();
at last
CFRelease(addressBook);

AddressBook kABPersonEmailProperty returns nil for some contacts

I am using the following code to retrieve the email addresses of persons picked by the user
ABMultiValueRef emailMultiValue = ABRecordCopyValue(person, kABPersonEmailProperty);
NSArray *emailAddresses = (__bridge NSArray*)ABMultiValueCopyArrayOfAllValues(emailMultiValue);
Everything has worked fine since iOS6 and Facebook integration came. When I choose a contact that is not linked to a Facebook account (this means that I'm not friend on FB with this contact) everything works fine and I retrieve 2 email addresses (work and home).
But when I select a contact that is linked to me via Facebook (this contact has also a home and work email address plus the Facebook email address in the contact browser) the emailAddresses array is nil.
Does anyone had this problem before or has any hints on this?
Thanks in advance!
To filter out the Facebook contacts, you can check the kABSourceTypeProperty of the person. If it returns kABSourceTypeCardDAV, it may be from Facebook, but it most certainly not a local contact (which would return kABSourceTypeLocal).
Unfortunately, as of iOS 6, there is not a Facebook specific source type, so using kABSourceTypeCardDAV may filter out other sources that you may actually care about. In any case, here is some code;
ABRecordRef source = ABPersonCopySource(person);
NSNumber *sourceTypeRef = (__bridge NSNumber *)((CFNumberRef)ABRecordCopyValue(source, kABSourceTypeProperty)
if ([sourceTypeRef intValue] == kABSourceTypeCardDAV)
; // this is probably, maybe, could be a Facebook contact
if ([sourceTypeRef intValue] == kABSourceTypeLocal)
; // this is definitely a local contact
if ([sourceTypeRef intValue] == kABSourceTypeExchange)
; // this is from an exchange server
// etc...
I sortof found a way to check if a person is a facebook contact. In my case the problem was that facebook contacts are not editable, so I didnt want to show them in the "addressbook updater" function Im making.
(in the addressbook app from apple, you can edit facebook contacts, but what the app actually does is to create a new contact with the new information and links it to the facebook contact. I couldnt do this, because linking is not an public API in iOS.)
So to find those facebook contacts that I want to ignore, I just check if ABRecordSetValue returns true when I set the firstname to the current firstname, like this.
bool didset = ABRecordSetValue(person, kABPersonFirstNameProperty, ABRecordCopyValue(person, kABPersonFirstNameProperty), nil);
I found out that since iOS6 there are linked contacts (normal contact <-> facebook <-> twitter and so on...).
In order to get all emails of all linked contacts, the following post helped me:
iOS 6 address book empty kABPersonPhoneProperty
Hope it helps!

Import contact from Address book to label

Is there any way to import a contact field(name, mobile phone, address) in a label?
(for iPhone app)
Yes, you can do it using the AddressBook framework.
Apple has instructions on how to retrieve contacts from your address book here.
Once you have retrieved a contact as an ABRecordRef, you can use the method ABRecordCopyValue(ABRecordRef record, ABPropertyID property) to get string values.
For example, to get a phone number, you could say (assuming curPerson is an already obtained ABRecordRef):
NSString *mobilePhone = (NSString *)ABRecordCopyValue(curPerson, kABPersonPhoneMobileLabel);

AddressBook: how to get names of different Exchange sources

I want to get the name of all the Exchange sources that my iPhone is synced to. For e.g. if I sync my device to Gmail and Hotmail accounts using ActiveSync Exchange, in the native contacts, I can see different sections based on the different accounts that I have setup. When I try to programmatically do the same, the only information I get is sourceType (which is Exchange in both cases) and sourceName (which is "Contacts" in both cases). There's no way for me to differentiate which is the Gmail "Contacts" and which is the Hotmail "Contacts".
Anyone know how to do this? Here's my code:
CFArrayRef allSources = ABAddressBookCopyArrayOfAllSources(book);
for (CFIndex i = 0; i < CFArrayGetCount(allSources); i++) {
ABRecordRef source = (ABRecordRef)CFArrayGetValueAtIndex(allSources, i);
NSString *sourceTypeName = (NSString *)((CFStringRef)ABRecordCopyValue(source, kABSourceNameProperty));
NSLog(#"%#", sourceTypeName);
}
You can't do this in iOS it seems (yet)

Accessing native iPhone addressbook database and performing add and delete contacts?

In my application I need to implement the address book which should contains the native addressbook details, and the user should be able to add and delete from the address book and it should be updated in the native iphone addressbook.
I read somewhere that the iphone native address book database is accesible. In documentation also I saw that addContact and Delete API's are exposed to addressbook.
Can anyone please tell me how can I access the native AddressBook of the iphone, and..
how to add and delete contacts from the address book?
Can anyone post the sample code for this?
You need to use ABRecords and ABAddressBook. For example, adding can be done:
#import <AddressBook/AddressBook.h>
...
ABRecordRef record = ABPersonCreate();
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordSetValue(record, kABPersonFirstNameProperty, CFSTR("Kevin"), NULL);
ABRecordSetValue(record, kABPersonLastNameProperty, CFSTR("Sylvestre"), NULL);
ABAddressBookAddRecord(addressBook, record, NULL);
ABAddressBookSave(addressBook, NULL);
It is important that you add the AddressBook.Framework to your project (right click on 'Frameworks' > 'Add' > 'Existing Frameworks'). The documentation should give you enough to figure out how to remove, etc.