How Does Square's Cardcase App Automatically Populate the User's Details from the Address Book

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

}
}

How does Path app know my phone number in the registration process

Moving my comments into an answer :)

As I've stated above, this is a duplicate of How does Square's CardCase app automatically populate the user's details from the address book?

Because Path asks beforehand for the first and last name it's easy to search for the contact in the address book. Of course one has to handle the case when a) no contact or b) multiple contacts are found. In both this cases I'd probably go with standard input fields, because for the "no contact found" case you need those anyway.

How common it is to have a contact with it's own name I don't know, but according to the Fact that Path and other apps are doing it the same way I suppose it's worth taking the risk :) AFAIK MacOS X automatically creates a contact with my name in the Address Book, but really can't recall if iOS has the same behavior.

Is it possible to get the current user's address book record?

I know on a Mac you can use [[ABAddressBook sharedAddressBook] me]. For iOS, take a look at how Square's CardCase app does it.

Is it possible to find user’s contacts that also have Apple devices?

This is now possible using CloudKit and the CKDiscoverAllContactsOperation class. You only get the user’s contacts that also use the same app and have given an explicit permission, but for some use cases it’s a nice start.

iOS6 Add some additional information to ABAdressbook

iOS provides no facility for storing extra data in the Address Book.

The most straightforward way for keeping extra contact-related data is to store it in your app, using the RecordID of the ABPerson record. However, be warned that those identifiers can disappear (if a record is deleted from the AddressBook) or even change (if the use changes the synchronization configuration e.g. for iCloud). If you really need to keep extra data matched up with ABPerson records then you can grab a copy of the CompositeName property (this is a derived value constructed from first name, last name etc) and then if your cached RecordID lookup fails, then hunt for a record with a matching CompositeName.

If all you want to do is just determine if an ABPerson record has changed, then perhaps it would be sufficient to just cache a value for ModificationDateProperty and compare that to the new value?

Programmatically Request Access to Contacts

As per this documentation on apple's site (scroll down to Privacy in the middle of the page), access to the address book must be granted before it can be access programmatically. Here is what I ended up doing.

  #import <AddressBookUI/AddressBookUI.h>

// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add the contact
[self _addContactToAddressBook];
} else {
// User denied access
// Display an alert telling user the contact could not be added
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self _addContactToAddressBook];
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}

Update For iOS 9 and later:

From Apple website :

Important

The Address Book UI framework is deprecated in iOS 9. Use the APIs defined in the ContactsUI framework instead. To learn more, see ContactsUI



Related Topics



Leave a reply



Submit