2015-10-11 86 views
7

我有一個需要訪問聯繫人選取器視圖控制器的iOS應用程序,以便允許用戶選擇聯繫人屬性,例如電子郵件地址/電子郵件地址的電話號碼。從CNContactProperty提取電子郵件 - iOS 9

我現在遇到的問題是我無法弄清楚如何解析返回的數據。我已經使用了contactPicker didSelectContactProperty方法,但我無法解析我需要的數據。

-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty { 

    CNLabeledValue *test = contactProperty.contact.emailAddresses.firstObject; 
    NSLog(@"%@", test); 

    NSLog(@"%@", contactProperty.contact.phoneNumbers); 
} 

如果你運行你獲得以下響應上面的代碼:

2015-10-11 13:30:07.059 Actions[516:212765] <CNLabeledValue: 0x13656d090: identifier=21F2B1B2-8158-466B-9224-E2036CA07D28, label=_$!<Other>!$_, [email protected]> 2015-10-11 13:30:07.061 App_Name[516:212765] (
    "<CNLabeledValue: 0x13672a500: identifier=6697A0E9-3B91-4566-B26E-83B87979F816, label=_$!<Main>!$_, value=<CNPhoneNumber: 0x13672a660: countryCode=gb, digits=08000391010>>") 

那是偉大的,但我怎樣提取我從它需要的數據?爲什麼NSLog語句以奇怪的格式返回數據?

謝謝你的時間,丹。

回答

14

返回的值是CNLabeledValue類。爲了從他們那裏得到的值,比方說,在電子郵件,做這個

CNLabeledValue *emailValue = contactProperty.contact.emailAddresses.firstObject; 
NSString *emailString = email.value; 

如果你想要一個電話號碼的價值,這是你將如何檢索

CNLabeledValue *phoneNumberValue = contactProperty.contact.phoneNumbers.firstObject; 
CNPhoneNumber *phoneNumber = phoneNumberValue.value; 
NSString *phoneNumberString = phoneNumber.stringValue; 

因爲返回的值是CNLabeledValue,你也能獲取電話號碼或電子郵件的標籤

NSString *emailLabel = emailValue.label; //This may be 'Work', 'Home', etc. 
NSString *phoneNumberLabel = phoneNumberValue.label; 
+0

啊我看到了。非常感謝。我剛從使用舊的AddressBook框架升級,所以我一直在努力。再次感謝:) – Supertecnoboff

+1

只有一個問題,我期待用戶選擇一個電話號碼的電子郵件地址。我怎樣才能檢查他們選擇了什麼? – Supertecnoboff

+2

我認爲這些信息將存儲在'contactProperty.value','contactProperty.label','contactProperty.key'等等中。我會測試每一個,看看它輸出了什麼,因爲我從來沒有用過親自。 –

0
Here is swift version of Chris answer : 

func fatchContacts(store : CNContactStore) { 
    do 
    { 
    let groups = try store.groups(matching: nil) 
    let predicate = CNContact.predicateForContactsInGroup(withIdentifier: groups[0].identifier) 
    //let predicate = CNContact.predicateForContactsMatchingName("John") 
     let keyToFatch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName) ,CNContactEmailAddressesKey] as [Any] 
    let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: keyToFatch as! [CNKeyDescriptor])   //------------------------------------------------------ 
    //-------------Get Here----------------------------------------- 
    print(contacts) 
    print(contacts[0]) 
     let formatter = CNContactFormatter() 
     print(formatter.string(from: contacts[0])) 
     print(contacts[0].givenName) 
     print(contacts[0].emailAddresses) 
     let emailValue : CNLabeledValue = contacts[0].emailAddresses.first!; 
     let email = emailValue.value 
     print(email) 




    } 
    catch{ 

    } 
} 


Just pass the CNContactStore object   
0

對於迅速3.0:

public func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) 
{ 
     if let emailValue : CNLabeledValue = contact.emailAddresses.first 
    { 
     txtEmail.text = emailValue.value as String 
    } 
    if let phoneNumber : CNLabeledValue = contact.phoneNumbers.first 
    { 
     txtMobno.text = phoneNumber.value.stringValue 
    } 
    txtFname.text = contact.givenName + " " + contact.familyName 

}