问题
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.
回答1:
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];
}
}
回答2:
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.
回答3:
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];
回答4:
I had the same issue, but in Swift, and used Nik Burns suggestion and wrote a blog post about it: http://paulpeelen.com/2016/01/20/prefill-an-signup-form-in-ios-using-swift-and-cncontact/
This is basically the end result in Swift:
import Contacts
...
@IBOutlet weak var nameFirst: UILabel!
@IBOutlet weak var nameLast: UILabel!
@IBOutlet weak var email: UILabel!
@IBOutlet weak var phoneNo: UILabel!
@IBOutlet weak var address: UILabel!
/**
Search the addressbook for the contact
*/
private func getMe() {
let ownerName = getOwnerName()
let store = CNContactStore()
do {
// Ask permission from the addressbook and search for the user using the owners name
let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName(ownerName), keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactPostalAddressesKey, CNContactEmailAddressesKey])
//assuming contain at least one contact
if let contact = contacts.first {
// Checking if phone number is available for the given contact.
if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
// Populate the fields
populateUsingContact(contact)
} else {
//Refetch the keys
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactPostalAddressesKey, CNContactEmailAddressesKey]
let refetchedContact = try store.unifiedContactWithIdentifier(contact.identifier, keysToFetch: keysToFetch)
// Populate the fields
populateUsingContact(refetchedContact)
}
}
} catch let e as NSError {
print(e.localizedDescription)
}
}
/**
Get the device owners name
- returns: The owners name
*/
private func getOwnerName() -> String {
// Get the device owners name
var ownerName = UIDevice.currentDevice().name.trimmedString.stringByReplacingOccurrencesOfString("'", withString: "")
// Get the model of the device
let model = UIDevice.currentDevice().model
// Remove the device name from the owners name
if let t = ownerName.rangeOfString("s \(model)") {
ownerName = ownerName.substringToIndex(t.startIndex)
}
return ownerName.trimmedString
}
/**
Populate the fields using a contact
- parameter contact: The contact to use
*/
private func populateUsingContact(contact: CNContact) {
// Set the name fields
nameFirst.text = contact.givenName
nameLast.text = contact.familyName
// Check if there is an address available, it might be empty
if (contact.isKeyAvailable(CNContactPostalAddressesKey)) {
if let
addrv = contact.postalAddresses.first,
addr = addrv.value as? CNPostalAddress where addrv.value is CNPostalAddress
{
let address = "\(addr.street)\n\(addr.postalCode) \(addr.city)\n\(addr.country)"
self.address.text = address
}
}
// Check if there is a phonenumber available, it might be empty
if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
if let
phonenumberValue = contact.phoneNumbers.first,
pn = phonenumberValue.value as? CNPhoneNumber where phonenumberValue.value is CNPhoneNumber
{
phoneNo.text = pn.stringValue
}
}
}
And the extension:
extension String {
/// Trim a string
var trimmedString: String {
return stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}
}
回答5:
No you can
http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Introduction.html#//apple_ref/doc/uid/TP40007744
I know it was available in 4.X; not just a 5.0 feature... Not sure if it was available earlier.
来源:https://stackoverflow.com/questions/8000927/how-does-squares-cardcase-app-automatically-populate-the-users-details-from-th