Tuesday, January 24, 2012

iOS Address Book fun

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;

8 comments:

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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!

    ReplyDelete
  4. 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

    ReplyDelete
  5. How to achieve this using Delphi XE4 ?

    ReplyDelete
  6. Thank you pertaining to giving this kind of with all of us you actually realize what you really are conversing about! Book-marked. You need to furthermore seek the advice of this web page Implies). We can easily use a weblink business contract in between you

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

    ReplyDelete
  8. Supercharge your marketing campaign with our social media promotion services. Buy high quality likes/followers/views at highly competitive prices and with fast delivery.

    Get 50,000 YouTube Views from:- http://khalaghor.com/youtube.html

    You can also order smaller or bigger package from our official website.

    ReplyDelete