通訊錄--(iOS9專屬的方法)

來源:互聯網
上載者:User

標籤:

匯入庫檔案   #import <ContactsUI/ContactsUI.h>  

#pragma mark iOS9 新出的點擊通訊錄的擷取資訊的辦法

#pragma mark - 先彈出連絡人控制器

- (IBAction)ios9Clicked:(id)sender {

    

    // 1. 建立控制器

    CNContactPickerViewController * picker = [CNContactPickerViewController new];

    // 2. 設定代理

    picker.delegate = self;

    // 3. 設定相關屬性,謂詞篩選email地址是@mac.com的連絡人

    picker.predicateForSelectionOfProperty = [NSPredicate predicateWithFormat:@"(key == ‘emailAddresses‘) AND (value LIKE ‘*@mac.com‘)"];

    //謂詞篩選email地址數等於1的連絡人

    picker.predicateForSelectionOfContact = [NSPredicate predicateWithFormat:@"[email protected] == 1"];

    // 4. 彈出

    [self presentViewController: picker  animated:YES completion:nil];

    

    

    

}

 

#pragma mark - 取消選中連絡人的時候調用,點擊右上方的cancel時候觸發,而不是picker的所有dismiss動作中都會觸發。在多選模式下,cancel在done的左側。

- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {

    

    NSLog(@"mxs點擊了取消");

    

}

//其他四個代理方法只要實現其中一個就行了。分別為單選和多選兩組,都實現的時候,多選優先執行,單選不執行。特別要注意的是predicateForEnablingContact,predicateForSelectionOfContact,predicateForSelectionOfProperty這三組屬性會影響它們的動作。predicateForEnablingContact返回YES的連絡人才是可互動的,預設連絡人都是可互動的。

//原文連結:http://www.jianshu.com/p/e6b7cb1eca9e

 

#pragma mark - 選擇連絡人的時候調用 (如果predicateForSelectionOfContact屬性沒被設定或符合篩選條件,如不符合則不會觸發該方法並進入連絡人詳情頁)

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {

    //1. 擷取姓名 ,givenName == firstName

    NSLog(@"givenName: %@, familyName: %@", contact.givenName, contact.familyName);

    //2. 擷取電話,泛型,會在數組遍曆時幫很大的忙

    for (CNLabeledValue *labeledValue in contact.phoneNumbers) {

        NSLog(@"label: %@",labeledValue.label);

        CNPhoneNumber *phoneNumber = labeledValue.value;

        NSLog(@"phoneNumber: %@",phoneNumber.stringValue);

    }

}

#pragma mark - 實現了此方法, 就可以選擇多個連絡人,該方法在點擊done按鈕時觸發,注意:該方法不受predicateForSelectionOfContact屬性影響!

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts {

    for (CNContact *contact in contacts) {

        NSLog(@"givenName: %@, familyName: %@", contact.givenName, contact.familyName);

        //2. 擷取電話,泛型,會在數組遍曆是幫很大的忙

        for (CNLabeledValue *labeledValue in contact.phoneNumbers) {

            NSLog(@"label: %@",labeledValue.label);

            CNPhoneNumber *phoneNumber = labeledValue.value;

            NSLog(@"phoneNumber: %@",phoneNumber.stringValue);

        }

    }

}

 

#pragma mark - 點擊某個連絡人的某個屬性(property)時觸發並返回該連絡人屬性(contactProperty)。只實現該方法時,可以進入到連絡人詳情頁面(如果predicateForSelectionOfProperty屬性沒被設定或符合篩選條件,如不符合會觸發預設操作,即打電話,發郵件等)。

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

    

}

 

#pragma mark - 只實現該方法時,停留在多選模式下的連絡人清單頁面(如果predicateForSelectionOfProperty屬性沒被設定或符合篩選條件,該連絡人才能被選中),在點擊done按鈕的時候觸發,返回的contactProperties中只包含選中的contactProperties,沒選中的話返回空。

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty *> *)contactProperties {

    // 迴圈列印出所有選中的連絡人名字

    for (CNContactProperty *contactProperty in contactProperties) {

        NSLog(@"%@",contactProperty.contact.givenName);

    }

 

}

通訊錄--(iOS9專屬的方法)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.