通讯录

系统通讯录

  • AddressBook(iOS9之前)

引入头文件

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

遵循代理

<ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate, ABPersonViewControllerDelegate>

实现代理方法

#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 <ContactsUI/ContactsUI.h>  

// 遵循代理

<CNContactPickerDelegate>

推荐

系统权限

通讯录

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简介: 前段时间公司需要iOS端获取手机通讯录,然后通过MFi(后续将写一写关于BLE以及MFi的相关文章)同步到...
    水丿果糖阅读 1,673评论 0 1
  • 如何访问用户的通讯录 在iOS中,有2个框架可以访问用户的通讯录 AddressBookUI.framework ...
    JonesCxy阅读 809评论 0 2
  • 通讯录简介 通讯录使用场景: 电商类的 App,设置收货人电话号码。 即时通讯类 App,添加手机联系人好友。 通...
    LeeJay阅读 24,959评论 43 107
  • 阳光已透过玻璃投射出它灿烂的影子。尽管冬已退去,但它余留的温度还在。春看到冬的恋恋不舍不忍离去之态,便不忍上前打扰...
    以琳阅读 112评论 0 0