一、 調(diào)用系統(tǒng)通訊錄,獲取聯(lián)系人信息
-
iOS9 之前的 <AddressBook/AddressBook.h> 和 <AddressBookUI/AddressBookUI.h> 框架
常用的一個(gè)代理方法
// Called after a property has been selected by the user.
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0);
-
iOS9 之后的 <Contacts/Contacts.h> 和 <ContactsUI/ContactsUI.h>框架
常用的兩個(gè)代理方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;
當(dāng)上面兩個(gè)代理方法都存在時(shí), 只會執(zhí)行后者。
-
若只是獲取姓名/電話信息, 用前者。 獲取的是 contactProperty
// 通訊錄列表 - 點(diǎn)擊某個(gè)聯(lián)系人 - 詳情頁 - 點(diǎn)擊一個(gè)號碼, 返回
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
/** 姓名 */
NSString *personName = [NSString stringWithFormat:@"%@%@", contactProperty.contact.familyName, contactProperty.contact.givenName];
/** 電話 */
NSString *phoneNumber = [contactProperty.value stringValue];
}
-
若要進(jìn)入詳情頁,撥打電話/編輯等,用后者。獲取的是: contact
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
// 選中聯(lián)系人后跳轉(zhuǎn)詳情頁, 跟從手機(jī)系統(tǒng)進(jìn)入一樣, 可以撥打電話、編輯聯(lián)系人.
CNContactViewController *contactVC = [CNContactViewController viewControllerForContact:contact];
[self.navigationController pushViewController:contactVC animated:YES];
/**
* 還可以通過viewControllerForNewContact新增聯(lián)系人
* 注意:通過其他方式進(jìn)入詳情頁, 用 modal
*/
}
二、發(fā)信息
1. 導(dǎo)入 MessageUI 框架: #import <MessageUI/MessageUI.h>
2. 服從協(xié)議:MFMessageComposeViewControllerDelegate
3. 配置方法,指定代理,并實(shí)現(xiàn)代理方法
/** 群發(fā)/單發(fā) 指定信息 */
- (void)sendContacts:(NSArray*)phoneNumbers message:(NSString *)message {
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if(messageClass != nil){
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc]init];
messageVC.messageComposeDelegate = self;
messageVC.body = message;
messageVC.recipients = phoneNumbers;
[self presentViewController:messageVC animated:YES completion:nil];
}else {
// Have error here ...
}
}
/** 發(fā)送信息后的回調(diào)方法 **/
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:^{}];
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultSent:
break;
case MessageComposeResultFailed:
break;
default:
break;
}
}