I wish to search in the iphone AddressBook through my app using the number as the key and then retrieve the image associated to that contact and display it on the UIImageView.
I tried using ABAddressBook framework but was clueless to proceed.
Can anyone please suggest me the solutions or any alternative path that I can follow. Any code snippet would also be of great help!!
Any form of help would be highly appreciable.
Thanks in advance
The AB framework can be a real pain at times. But it breaks down to a series of pretty simple operations. First, you have to create an ABAddressBook instance:
Then you’ll want to make a copy of the array of all people in the address book, and step through them looking for the data you want:
Inside your loop, you’ll probably want to get a reference to the individual person:
Then you want to compare the number you have (lets call that
inNumber) to every phone number associated with that particular person. To do that, you first need a list of all the person’s phone numbers:Then, of course, you’ll need to have an inner loop that loops over each of the individual person’s phone numbers:
Since the phone numbers have both numbers and labels associated with them, you’ll need to extract the actual phone number string as an NSString:
Now you can finally compare numbers! Do so with the standard NSString comparison methods, but remember that you need to worry about formatting, etc.
Once you find the person who has a phone number matching
inNumber, you’ll want the extract that person’s image into aUIImage:When it comes time to exit, you’ll need to clean up memory. A general rule of thumb for the AB framework is that anything with
Getin the function name you don’t need to release, and anything withCopyorCreate, you do need to release. So, in this case you’ll need toCFRelease()phonelist,allPeople, andaddressbook, but notnumPeople,person, ornumPhones.