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.
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];
}
}
Since the changes that require apps to ask for permissions to access the address book this method no longer works. Nik Burns' answer worked great, but it needs access to the address book.
I downloaded the Square Wallet (which is the successor to Square Card Case references in the original question) and it no longer pre-populates the sign up form. Path also used to do the device name address book trick but it also no longer pre-populates the sign up form.
Using the above method, you could still pre-populate the sign up form after requesting Contacts access but it loses the desired "magic" experience.
Since I was not happy with having to use the device name I chose to use the first record of the address book which corresponds to "ME"
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, 0 );
ABContact * contact = [ABContact contactWithRecord:ref];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With