要操作 iOS 的系統通訊錄,需要引入兩個框架:AddressBook.framework 負責底層資料讀寫,AddressBookUI.framework 提供系統原生的選擇介面。
@interface HMTMainViewController () @property (nonatomic, strong) ABPeoplePickerNavigationController *personPickVC; @property (nonatomic, strong) UIWebView *phoneCallWebView; @end
在 viewDidLoad 中放一顆按鈕作為入口:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(100, 70, 120, 40); [button setTitle:@"選擇聯繫人" forState:UIControlStateNormal]; [button addTarget:self action:@selector(didClickSelectAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
- (void)didClickSelectAction {
self.personPickVC = [[ABPeoplePickerNavigationController alloc] init];
_personPickVC.peoplePickerDelegate = self;
// 只顯示電話欄位
_personPickVC.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]];
[self presentViewController:_personPickVC animated:YES completion:NULL];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[peoplePicker dismissViewControllerAnimated:YES completion:^{
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(
person, kABPersonFirstNameProperty);
NSLog(@"person = %@", firstName);
// 一個人可能有多筆電話,必須用 multiValue 逐一取出
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int i = 0; i < ABMultiValueGetCount(phones); i++) {
NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"telephone = %@", phone);
}
}];
return YES;
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[peoplePicker dismissViewControllerAnimated:YES completion:NULL];
}
這裡的 __bridge 是重點。AddressBook 是純 C 框架,回傳的是 CFStringRef,必須橋接轉型才能當作 NSString 使用;同時 ABRecordCopyValue 遵循 Create Rule,取得的內容需要自行釋放,實務上可用 CFRelease 收尾。
- (void)getAllPerson {
CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook);
for (id person in (__bridge NSArray *)allPerson) {
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(
(__bridge ABRecordRef)person, kABPersonFirstNameProperty);
NSLog(@"firstName = %@", firstName);
ABMultiValueRef phones = ABRecordCopyValue(
(__bridge ABRecordRef)person, kABPersonPhoneProperty);
for (int i = 0; i < ABMultiValueGetCount(phones); i++) {
NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"telephone = %@", phone);
}
}
}
- (void)addPerson {
ABAddressBookRef addressBook = self.personPickVC.addressBook;
ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person, kABPersonFirstNameProperty, @"胡", nil);
ABRecordSetValue(person, kABPersonLastNameProperty, @"明濤", nil);
ABMutableMultiValueRef mulRef = ABMultiValueCreateMutable(kABStringPropertyType);
ABMultiValueAddValueAndLabel(mulRef, @"18690231234", kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, mulRef, NULL);
ABAddressBookAddRecord(addressBook, person, NULL);
ABAddressBookSave(addressBook, NULL);
}
- (void)deletePerson {
CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook);
for (id person in (__bridge NSArray *)allPerson) {
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(
(__bridge ABRecordRef)person, kABPersonFirstNameProperty);
if ([firstName isEqualToString:@"胡"]) {
ABAddressBookRemoveRecord(self.personPickVC.addressBook,
(__bridge ABRecordRef)person, NULL);
}
}
ABAddressBookSave(self.personPickVC.addressBook, NULL);
}
新增與刪除都必須以 ABAddressBookSave 收尾,否則改動不會落地。ABMutableMultiValueRef 這層包裝是為了承載「同一筆聯絡人多組號碼」的結構。
- (void)dialPhoneNumber:(NSString *)phoneNumber {
// 寫法一:由 UIWebView 觸發系統撥號
NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]];
self.phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
[self.phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
// 寫法二:直接開啟 telprompt,撥完會自動跳回 App
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10086"]];
}
兩種寫法的差異在於「撥完之後去哪裡」。tel: 會離開 App 進入電話應用,而 telprompt: 會在通話結束後自動回到你的 App,對於需要連續追蹤狀態的流程體驗更好。要注意的是 telprompt 屬於非公開行為,上架審核時建議改用 tel。
最後提醒:iOS 8 之後讀寫通訊錄已改用 Contacts 框架(CNContactStore),且無論新舊 API 都需要使用者在系統設定中明確授權。若在未請求權限的情況下直接存取,實際取得的資料會是空的。