iOS Address Book fun

Posted by on in Blogs
Getting data out of the iOS address book is a lot harder than I thought... Must make component... ;)

Here's a Q&D routine I managed to eek out today. It simply prints all contacts and all their numbers to the log console.

Enjoy!
uses
iPhoneAll, AddressBook, CFArray, CFBase;

procedure DumpAddressBook;
var
addressBook : ABAddressBookRef;
allPeople : CFArrayRef;
nPeople : CFIndex;
ref : ABRecordRef;
i, j : Integer;
firstName : CFStringRef;
lastName : CFStringRef;
phoneNumber : CFStringRef;
l, v : CFStringRef;
S : NSString;
begin
addressBook := ABAddressBookCreate;
allPeople := ABAddressBookCopyArrayOfAllPeople(addressBook);
nPeople := ABAddressBookGetPersonCount(addressBook);

for i:=0 to nPeople-1 do begin
ref := CFArrayGetValueAtIndex(allPeople,i);

firstName := ABRecordCopyValue(ref,kABPersonFirstNameProperty);
lastName := ABRecordCopyValue(ref,kABPersonLastNameProperty);
if firstName <> nil then
if lastName <> nil then
S := NSString.stringWithFormat(NSSTR(PChar('%@, %@')),lastName,firstName)
else
S := NSString.stringWithFormat(NSSTR(PChar('%@')),firstName)
else
if lastName <> nil then
S := NSString.stringWithFormat(NSSTR(PChar('%@')),lastName);
NSLog(S);

phoneNumber := ABRecordCopyValue(ref, kABPersonPhoneProperty);
for j:=0 to ABMultiValueGetCount(phoneNumber)-1 do begin
l := ABMultiValueCopyLabelAtIndex(phoneNumber,j);
v := ABMultiValueCopyValueAtIndex(phoneNumber,j);
S := NSString.stringWithFormat(NSSTR(PChar('%@: %@')),l,v);
NSLog(S);
CFRelease(l);
CFRelease(v);
end;

if phoneNumber <> nil then
CFRelease(phoneNumber);
if firstName <> nil then
CFRelease(firstName);
if lastName <> nil then
CFRelease(lastName);
end;
end;


Comments

  • Guest
    Olecramoak Thursday, 8 March 2012

    That's really cool!

    Thanks a lot Anders! You cannot imagine how important your posts are to us firemonkey iOS developers... not much information of this kind anywhere else.

    Please keep investing time on it, it's VERY helpful.

    1.) By the way, is it possible to copy text to iPhones "clipboard"? For an example, copy a memo's text and paste it to an e-mail? I tried to send the e-mail directly with the memo's text (following your sample from right here) but got an error. Something about unicode string... If I could copy the text, and the user paste it wherever they want it would be great.

    2.) Right now, I'm trying to figure out how to use StoreKit (submitted 10 completely different apps to AppStore and they rejected it and forced me to consolidate all of them in a single container app and use in-app purchase. Filled an appeal to the review board and lost. Got to do it their way against my will). I'm looking at Apple's documentation (Obj-C) but cannot translate the sample code myself... really confusing.

  • Guest
    Anders Ohlsson Friday, 9 March 2012

    Unfortunately I haven't had time to play with any of what you ask for. I know it's possible. You may have to parse some framework headers to get at it. Not sure.

  • Guest
    Olecramoak Friday, 9 March 2012

    I read somewhere that storekit is included in iPhoneall. If it ally is, would parsing still be necessary?

    Could you recommend any material that I could use to study translation to obj-c to Delphi (i saw a delphi code somewhere with the original objc lines commented as reference)?

    Cheers!

  • Guest
    bruno Thursday, 5 July 2012

    Hi Anders, you know something about add address information to a contact ? (with NSDictionary/NSMutableDictionary). i will very glad if you can provide this . Thanks

  • Guest
    Ahmad Monday, 20 May 2013

    How to achieve this using Delphi XE4 ?

  • Guest
    Nick Wednesday, 4 December 2013

    Very useful information. Where to take the libraries of iPhoneAll, AddressBook?

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: