2011-11-03 25 views

回答

53

我能想出的唯一解決方案是使用設備名稱,然後搜索地址簿進行匹配。這假定有人會使用特定的命名約定。例如,我使用'Nik's iPhone'作爲我的設備名稱。我也是我地址簿中唯一的Nik,所以對於我的場景來說,使用之前的文本作爲所有者名稱效果很好。

它利用Erica Sadun的ABAddressBook非常方便的包裝,ABContactHelper。 我已經將枚舉代碼留在了,而不是在0處使用數組索引,因爲可能會返回少量的匹配項,因此您可以擴展以給用戶選擇「他們的」細節的選項。雖然不完全匹配方形案例解決方案效果很好。恕我直言。

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

    } 
} 
+5

我可以證實這似乎是他們如何做到的。如果將設備名稱更改爲地址簿中不存在的名稱,則「卡片大小寫」會顯示從設備名稱派生的新名稱。 – TomSwift

+1

太棒了。按規則播放的好戲法! – Genericrich

+1

它現在還在工作嗎?電話不會要求通訊錄的權限嗎? – Kamchatka

0
+0

還沒有看到它。文檔中的哪裏可以獲取用戶的帳戶信息? – Genericrich

+0

您仍然需要查詢它,在Mac OS X上沒有像.me這樣的方法。我看到有人提示它,即彈出人員選擇器,允許他們選擇自己而不是存儲ID。或者其他人已經獲得了電話號碼,然後查詢該電話號碼的地址簿等。 –

+0

是的,這不是Square應用程序的做法。仍然困惑。 – Genericrich

23

由於需要應用程序的變化,要求的權限來訪問地址簿這種方法不再起作用。 Nik Burns的答案效果很好,但它需要訪問地址簿。

我下載了方形錢包(這是Square Card Case參考文件在原始問題中的繼承者),它不再預先填寫註冊表單。路徑也用於執行設備名稱地址簿技巧,但它也不再預先填寫註冊表單。

使用上述方法,您仍然可以在requesting Contacts access之後預填充註冊表格,但它失去了所需的「魔術」體驗。

+4

這是這個問題的新「正確答案」。 – JOM

1

因爲我是不滿意不必使用我選擇使用的地址簿中的第一個記錄相當於「ME」

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

我有同樣的問題,該設備的名稱,但是在斯威夫特,並用聶伯恩斯建議,並寫了一篇博客文章吧:http://paulpeelen.com/2016/01/20/prefill-an-signup-form-in-ios-using-swift-and-cncontact/

這基本上是最終結果斯威夫特:

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()) 
    } 

} 
+0

奇怪的是我自己的電話號碼不在聯繫人列表中。這是新版iOS版本的新變化嗎?我是否必須手動添加自己或自動發生? – Isuru

相關問題