2015-10-11 8 views
7

मेरे पास एक आईओएस ऐप है जिसके लिए उपयोगकर्ता को संपर्क पिकर व्यू कंट्रोलर तक पहुंच की आवश्यकता है ताकि उपयोगकर्ता संपर्क पते का चयन कर सकें जैसे ईमेल पते/ईमेल पते के ईमेल पते।CNContactProperty से ईमेल निकालें - आईओएस 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>>") 

महान Thats, लेकिन मैं डेटा मैं इसे से की जरूरत है कि कैसे निकाल सकते हैं? एनएसएलओजी कथन एक अजीब प्रारूप में डेटा वापस क्यों कर रहे हैं?

आपके समय के लिए धन्यवाद, दान।

उत्तर

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

आह ठीक है मैं देखता हूं। बहुत बहुत धन्यवाद। मैंने अभी पुराने एड्रेसबुक ढांचे का उपयोग करने से अपग्रेड किया है, इसलिए मैं इसके साथ संघर्ष कर रहा था। धन्यवाद फिर से :) – 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 

}