系統(tǒng)通訊錄
- AddressBook(iOS9之前)
引入頭文件
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
遵循代理
<ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate, ABPersonViewControllerDelegate>
實現(xiàn)代理方法
#pragma mark 選擇聯(lián)系人
- (void)handleAddContact:(UIButton *)sender
{
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
peoplePicker.peoplePickerDelegate = self;
peoplePicker.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
[self presentViewController:peoplePicker animated:YES completion:nil];
}
#pragma mark 跳轉(zhuǎn)聯(lián)系人詳情
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person{
ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
personViewController.displayedPerson = person;
personViewController.personViewDelegate = self;
[peoplePicker pushViewController:personViewController animated:YES];
}
- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
// 如果點擊的是電話選項剃氧,不進行響應(yīng),就是在真機中阱缓,點擊電話cell,不會相應(yīng)撥打電話
return (property == kABPersonPhoneProperty)?false:true;
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
if (property == kABPersonPhoneProperty)
{
//獲取聯(lián)系人的姓名
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *firstNameStr = (__bridge_transfer NSString *)(firstName);
NSString *lastNameStr = (__bridge_transfer NSString *)(lastName);
NSString *name = @"";
// 小坑厢岂,10.0后如果用UIAddressBook,firstName和lastName居然反著
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0)
{
name = [NSString stringWithFormat:@"%@%@",lastNameStr?lastNameStr:@"",firstNameStr?firstNameStr:@""];
}
else
{
name = [NSString stringWithFormat:@"%@%@",firstNameStr?firstNameStr:@"",lastNameStr?lastNameStr:@""];
}
// 獲取手機號
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *phoneValue = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phones, identifier);
NSString *phoneNum1 = [phoneValue stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSString *phoneNum2 = [phoneNum1 stringByReplacingOccurrencesOfString:@"(" withString:@""];
NSString *phoneNum3 = [phoneNum2 stringByReplacingOccurrencesOfString:@")" withString:@""];
NSString *phoneNum4 = [phoneNum3 stringByReplacingOccurrencesOfString:@"(" withString:@""];
NSString *phoneNum5 = [phoneNum4 stringByReplacingOccurrencesOfString:@")" withString:@""];
_nameTextField.text = name;
_phoneTextField.text = phoneNum5;
CFRelease(phones);
}
NSLog(@"選擇了某個屬性");
}
- ContactsUI(iOS9之后)
引入頭文件
#import <ContactsUI/ContactsUI.h>
// 遵循代理
<CNContactPickerDelegate>