通讯录

系统通讯录

  • AddressBook(iOS9之前)

引入头文件

#import 
#import 

遵循代理


实现代理方法

#pragma mark 选择联系人
- (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 跳转联系人详情
- (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
{
    // 如果点击的是电话选项,不进行响应,就是在真机中,点击电话cell,不会相应拨打电话
    return (property == kABPersonPhoneProperty)?false:true;
}

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
    
    if (property == kABPersonPhoneProperty)
    {
        //获取联系人的姓名
        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   

// 遵循代理


推荐

系统权限

通讯录

iOS开发进阶-Contacts/ContactUI学习和使用

你可能感兴趣的:(通讯录)