所謂的iOS中的通訊錄(一)(自製簡易視圖版),ios通訊錄

來源:互聯網
上載者:User

所謂的iOS中的通訊錄(一)(自製簡易視圖版),ios通訊錄

  • 在iOS中,有兩個架構可以訪問使用者的通訊錄
    • 提供了連絡人清單介面、連絡人詳情介面、新增連絡人...介面等
    • 一般用於選擇連絡人

        2.  AddressBook.framework  

      • 純C語言的API,僅僅是擷取連絡人資料
      • 沒有提供UI介面顯示,需要自己搭建連絡人展示介面
      • 裡面的資料類型大部分基於Core Foundation架構,使用起來效果不佳
  • 從iOS6開始。需要得到使用者的授權才能訪問通訊錄,因此在使用之前,需要檢查使用者是否已經授權

 

1、產生連絡人控制器

 1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 2 { 3     // 1.建立選擇連絡人的介面 4     ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init]; 5      6     // 2.設定代理 7     ppnc.peoplePickerDelegate = self; 8      9     // 3.彈出選擇連絡人介面10     [self presentViewController:ppnc animated:YES completion:nil];11 }

2、代理方法

  1)點擊連絡人時調用

 1 // 系統注釋:Called after a person has been selected by the user. 2 // 直譯:一個人被選中後被稱為使用者後調用 3 // 意譯:選中某一個連絡人的時候,會執行該代理方法 4 // 方法:如果實現了該方法,那麼就不會進入連絡人的詳細介面 5 - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0) { 6      7     // 1.擷取選中連絡人的姓名(姓lastname和名firstname) 8     CFStringRef firstname = ABRecordCopyValue(person, kABPersonFirstNameProperty); 9     CFStringRef lastname = ABRecordCopyValue(person, kABPersonLastNameProperty);10     NSString *firstName = (__bridge_transfer NSString *)(firstname);11     NSString *lastName = (__bridge_transfer NSString *)(lastname);12     NSLog(@"%@ %@", firstName, lastName);13     14     // 2.擷取連絡人的電話號碼15     ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);16     CFIndex count = ABMultiValueGetCount(phones);17     for (CFIndex i = 0; i < count; i++) {18         NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);19         NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);20         NSLog(@"label : %@ value : %@", phoneLabel, phoneValue);21     }22     23     // 3.釋放不再使用的對象24     CFRelease(phones);25     26 }

  2)點擊連絡人屬性時調用

 1 // Called after a property has been selected by the user. 2 // 直譯:一個屬性被使用者選定後調用。 3 // 意譯:選中某一個連絡人的屬性的時候,會執行該代理方法 4 // 方法:如果實現了該方法,那麼選中一個連絡人的屬性時,就會推出控制器.不會進入下一個頁面 5 // 參數: arg1:property 選中的屬性 identifier : 每一個屬性都由一個對應標示 6 - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0) { 7      8     NSLog(@"選擇了某一個連絡人的某一個屬性"); 9     10 }

  3)點擊取消按鈕調用

 

1 // Called after the user has pressed cancel.2 // 直譯:取消被選中後被稱為使用者後調用3 // 意譯:選中取消按鈕的時候,會執行該代理方法4 - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {5     6     NSLog(@"使用者點擊了取消按鈕");7     8 }

 

                                                       未完待續~~~~~

                                                    by:Coder丶PSS

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.