Square的CardCase应用程序如何从地址簿中自动填充用户的详细信息?

Square的新款卡片箱iOS应用程序具有“创build帐户”function。 点击它,它会显示用户从地址簿中input的预设表单。

这怎么可能? 有人知道吗? 我认为这是不可能的,以这种方式获取用户的信息。 这不是一个iOS 5.0的东西,afaict。

我唯一可以想到的解决scheme是使用设备名称,然后search地址簿进行匹配。 这假定有人会使用特定的命名约定。 我例如使用“Nik的iPhone”作为我的设备名称。 我也是我的地址簿中唯一的Nik,所以对于我的场景来说,使用之前的文本作为所有者的名字是很好的。

它利用EricAddison ABContactHelper的ABAddressBook非常方便的包装。 我已经把枚举代码留在了,而不是在0使用数组索引,因为可能会返回less量的匹配,所以你可以展开给用户一个select“他们的”细节的选项。 虽然不完全匹配scheme案例解决scheme运作良好。 恕我直言。

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

由于需要应用程序请求访问通讯簿权限的更改,此方法不再有效 。 Nik Burns的答案很好,但需要访问地址簿。

我下载了方形钱包(原来的问题是Square Card Case引用的inheritance者),并且不再预先填写registry单。 path也用来做设备名称地址簿技巧,但它也不再预先填写registry单。

使用上述方法,您仍然可以 请求“通讯录”访问权限之前预先填写registry单但会失去所需的“神奇”体验。

由于我不喜欢使用设备名称,我select使用地址簿中对应于“我”的第一条logging

 ABAddressBookRef addressBook = ABAddressBookCreate( ); CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook ); ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, 0 ); ABContact * contact = [ABContact contactWithRecord:ref]; 

我有同样的问题,但在Swift中,并使用Nik Burns的build议,并写了一篇关于它的博客文章: http : //paulpeelen.com/2016/01/20/prefill-an-signup-form-in-ios-using -swift和- cncontact /

这基本上是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 } } } 

而延伸:

 extension String { /// Trim a string var trimmedString: String { return stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) } } 
Interesting Posts