2015-10-11 11 views
7

Kullanıcının e-posta adresi/iminsaj e-posta adresleri gibi bir kişi özelliğini seçmesine izin vermek için Kişiler seçici görünümü denetleyicisine erişmesi gereken bir iOS uygulamasına sahibim.CNContactProperty - iOS'den e-posta ayıklama 9

Şu anda sahip olduğum sorun, döndürülen verilerin nasıl ayrıştırılacağını anlayamıyorum. contactPicker didSelectContactProperty yöntemini kullandım, ancak ihtiyacım olan verileri ayrıştıramıyorum.

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

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

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

Eğer şu yanıtı almak Yukarıdaki kodu çalıştırırsanız:

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>>") 

harika Thats ama nasıl ben ondan gereken verileri ayıklamak? NSLog ifadeleri neden verileri garip bir biçimde döndürüyor?

Zaman ayırdığınız için teşekkürler, Dan.

cevap

14

Döndürülen değerler CNLabeledValue sınıfından. Bir telefon numarası istedi değeri, bu almak nasıl olursa e-postalar, diyelim, onlardan değeri elde etmek için, bu

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

yapmak Çünkü

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

döndürülen değer de telefon numarası veya e-postanın etiketi almak edebiliyoruz, bir CNLabeledValue olduğunu

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

Ah doğru görüyorum. Çok teşekkürler. Daha eski AddressBook çerçevesini kullanıyorum, bu yüzden bununla mücadele ediyordum. Tekrar teşekkürler :) – Supertecnoboff

+1

Sadece bir soru, kullanıcının bir telefon numarasının bir e-posta adresini seçmesini bekliyorum. Seçtiklerini nasıl kontrol edebilirim? – Supertecnoboff

+2

Bu bilginin 'contactProperty.value',' contactProperty.label', 'contactProperty.key' vb. Yerlerde saklanacağını düşünüyorum. Bunları daha önce hiç kullanmadığım için bunlardan her birini test edip çıktılarını görebiliyordum Şahsen. –

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

Hızlı 3.0:

için
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 

}