How does Square's CardCase app automatically populate the user's details from the address book?

Genericrich picture Genericrich · Nov 3, 2011 · Viewed 16.1k times · Source

Square's new card case iOS app has a "Create Account" feature. Tap it and it shows a form PREPOPULATED with the user's entry from the Address book.

How is this possible? Anyone know? I thought this was unpossible, to get the user's info this way. It's not an iOS 5.0 thing, afaict.

Answer

Nik Burns picture Nik Burns · Nov 8, 2011

the only solution I could come up with was using the device name and then searching the addressbook for a match. this assumes someone would use a particular naming convention. I for example use 'Nik's iPhone' as my device name. I am also the only Nik in my addressbook, so for my scenario is works well to use the text prior to 's as the owner name.

It makes use of the very handy wrapper for ABAddressBook by Erica Sadun, ABContactHelper. I've left the enumeration code in instead of using array index at 0 as likely a small number of matches will be returned so you could expand to give the user an option to choose 'their' details. Which whilst not exactly matching the square case solution works well. imho.

NSString *firstname;
NSString *lastname;

NSString *ownerName = [[UIDevice currentDevice] name];

NSRange t = [ownerName rangeOfString:@"'s"];
if (t.location != NSNotFound) {
    ownerName = [ownerName substringToIndex:t.location];
} 

NSArray *matches = [ABContactsHelper contactsMatchingName:ownerName];
if(matches.count == 1){ 
    for (ABContact *contact in matches){
        firstname = [contact firstname];
        lastname = [contact lastname];

    }
}