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

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];

    }
}

Leave a Comment