您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 呼叫系統通訊錄:挑選聯絡人並取得電話

編輯:iOS 相關資訊

在 iOS 中,若需要讓使用者從系統通訊錄選擇聯絡人,可以使用 AddressBookUI 提供的 ABPeoplePickerNavigationController,而不是自行建立通訊錄列表。

第一步:匯入框架

專案需要引入 AddressBook 與 AddressBookUI 兩個框架,才能使用通訊錄與選擇器相關 API。

第二步:呈現聯絡人選擇器

ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
[self presentViewController:peoplePicker animated:YES completion:nil];

第三步:處理代理方法

當使用者選取某個電話屬性時,可透過 ABPeoplePickerNavigationControllerDelegate 的 didSelectPerson:property:identifier: 取得 ABRecordRef、ABPropertyID 與 ABMultiValueIdentifier,再複製對應電話值。

ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef, identifier);
CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef, index);
[self dismissViewControllerAnimated:YES completion:^{
    self.textField.text = (__bridge NSString *)value;
}];

這個做法適合 iOS 8 之後的專案;若需要支援更舊版本,則要改用其他 API 並注意不相容問題。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved