ABPeoplePickerNavigationController

联系人获取

//
//  ZYViewController.m
//  获取系统通讯录
//
//  Created by zm on 15-3-11.
//  Copyright (c) 2015年 ZM. All rights reserved.
//

#import "ZYViewController.h"

@interface ZYViewController ()

@end

@implementation ZYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
//获取系统通讯录
- (IBAction)contactsClick:(id)sender
{
    //创建通讯录对象
    //ABPeoplePickerNavigationController:UINavigationController
    //在创建的时候不用指定跟视图控制器,因为它自带的有跟视图控制器
    ABPeoplePickerNavigationController* peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
    //设置delegate
    peoplePicker.peoplePickerDelegate = self;
    //模态弹出
    [self presentViewController:peoplePicker animated:YES completion:nil];
    [peoplePicker release];

}
//////ABPeoplePickerNavigationControllerDelegate////
//点击cancel时调用的方法
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    //模态消失
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
//点击某个联系人的时候调用此方法
//作用:是否允许进一步操作(是否允许进入下一个界面)
//参数:peoplePicker:通讯录对象   person:哪个联系人
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    /*
    //获取这个(person)联系人的姓名
    //Composite:混合的,组合的,复合的
    NSString* name = (NSString* )ABRecordCopyCompositeName(person);
    _nameLabel.text = name;

    //获取联系人的电话号码等
    //首先获取这类信息的集合
    //参数:<#ABRecordRef record#> 联系人对象<#ABPropertyID property#>  种类标示符(比如:电话 kABPersonPhoneProperty  地址 kABPersonAddressProperty)
    ABMultiValueRef multiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
    //然后,去取相应的信息
    //参数:<#ABMultiValueRef multiValue#> 集合对象  <#CFIndex index#>  取这类信息的第几个信息
    NSString* phone = (NSString* )ABMultiValueCopyValueAtIndex(multiValueRef, 0);
    //最后显示出来
    _phoneLabel.text = phone;
    //[peoplePicker dismissViewControllerAnimated:YES completion:nil];
     */
    return YES;
}
//具体点击了某类信息的某条信息的时候调用此方法
//是否允许用户进行下一步操作(比如:点击电话号码时,是否允许拨号)
//参数:peoplePicker:通讯录对象  person:具体的某个联系人   property:哪种类型的信息(比如:电话,地址,emial)  identifier:标示符(比如:电话这类信息中:你是选择了home,还是work,靠identifier来区分)
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    //获取联系人的姓名
    NSString* name = (NSString* )ABRecordCopyCompositeName(person);
    _nameLabel.text = name;

    //获取电话号码
    //集合
    ABMultiValueRef ref = ABRecordCopyValue(person, property);
    //获取这个集合的元素个数
    int count = ABMultiValueGetCount(ref);
    NSLog(@"count = %d",count);
    //根据集合取出你选择的这条数据
    int number = ABMultiValueGetIndexForIdentifier(ref, identifier);
    NSLog(@"number = %d",number);
    NSString* phone = ABMultiValueCopyValueAtIndex(ref, number);
    //处理字符串
    phone = [self getString2:phone];
    NSLog(@"phone = %@",phone);
    _phoneLabel.text = phone;
    //模态消失
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    return YES;
}
//去掉电话号码中的不需要的符号
//方法一:
- (NSString* )getString1:(NSString* )info
{
    info = [info stringByReplacingOccurrencesOfString:@" " withString:@""];
    info = [info stringByReplacingOccurrencesOfString:@"(" withString:@""];
    info = [info stringByReplacingOccurrencesOfString:@")" withString:@""];
    info = [info stringByReplacingOccurrencesOfString:@"-" withString:@""];
    return info;
}
//方法二
- (NSString* )getString2:(NSString* )info
{
    NSMutableString* string = [[NSMutableString alloc] initWithString:info];
    NSRange range = [string rangeOfString:@" "];
    while (range.location != NSNotFound) {
        [string deleteCharactersInRange:range];
        range = [string rangeOfString:@" "];
    }

    range = [string rangeOfString:@"("];
    while (range.location != NSNotFound) {
        [string deleteCharactersInRange:range];
        range = [string rangeOfString:@"("];
    }

    range = [string rangeOfString:@")"];
    while (range.location != NSNotFound) {
        [string deleteCharactersInRange:range];
        range = [string rangeOfString:@")"];
    }

    range = [string rangeOfString:@"-"];
    while (range.location != NSNotFound) {
        [string deleteCharactersInRange:range];
        range = [string rangeOfString:@"-"];
    }
    return string;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_nameLabel release];
    [_phoneLabel release];
    [super dealloc];
}

@end
时间: 2024-10-08 16:07:23

ABPeoplePickerNavigationController的相关文章

iOS:ABPeoplePickerNavigationController系统通讯录使用

昨天因项目需求要访问系统通讯录获取电话号码,于是乎从一无所知,开始倒腾,倒腾了一下午,总算了弄好了.写这边博客是为了记录一下,自己下一次弄的时候就别在出错了.同时,有和我一样的菜鸟能够避免走一下弯路. 好了,言归正传,要访问系统的通讯录,首先需要添加AddressBook.framework和AddressBookUI.framework两个框架到你工程中build phase的"Link Binary With Libraries"之下,然后就可以开始了. 首先我们需要创建一个控制器

ios 8 联系人ABPeoplePickerNavigationController

一. ios 联系人ABPeoplePickerNavigationControllerDelegate方法,新添加下面两个联系人选中方法,适配iOS8需要实现 // Called after a person has been selected by the user. - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRec

通讯录--(适配iOS7/8/9)

导入库#import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> #pragma mark  点击 弹出通讯录 - (IBAction)contactClicked:(id)sender { //1. 创建联系人选择控制器 ABPeoplePickerNavigationController *picker = [ABPeoplePickerNavigationController new]

iOS开发--系统通讯录的访问与添加联系人

公司项目有访问通讯录的需求,所以开始了探索之路.从开始的一无所知,到知识的渐渐清晰.这一切要感谢广大无私分享的 “coder”,注:我是尊称的语气! 苹果提供了访问系统通讯录的框架,以便开发者对系统通讯录进行操作.(此demo为纯代码),想要访问通讯录,需要添加AddressBookUI.framework和AddressBook.framework两个框架,添加的地点这里就不在赘述了.在控制器内部首先import两个头文件,<AddressBook/AddressBook.h> 和 <

ios开发——实用技术篇Swift篇&amp;地址薄、短信、邮件

1 //返回按钮事件 2 @IBAction func backButtonClick() 3 { 4 self.navigationController?.popViewControllerAnimated(true) 5 } 6 7 //新增联系人 8 @IBAction func addPeople () 9 { 10 //取得电话薄句柄 11 var error:Unmanaged<CFError>? 12 var addressBook: ABAddressBookRef? = AB

iOS --调用系统通讯录

// 调用系统通讯录需要遵循两个代理ABPeoplePickerNavigationControllerDelegate,UINavigationControllerDelegate 相关类为ABPeoplePickerNavigationController // 系统通讯录自带导航栏,所有要model出来 // 初始化 ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController

IOS-通信录

1.概述: * 对于每一个移动设备而言,都有一个内置的数据库-----通讯录. * 在IOS上,通讯录放在SQLite3数据库中. * 由于不同应用之间不能直接访问,我们想要实现对数据库的访问,必须使用苹果开放的API. * 苹果公司对数据访问开放的API是AddressBook(低级API)和AddressBookUI(高级API). * 为了安全考虑,访问通讯录需要经过用户授权,仅需一次授权即可. 2.AddressBook 和 AddressBookUI的区别: * AddressBook

iOS框架—使用地址簿

一.为何支持地址簿很重要: 当开发iOS软件的时候,就与用户的移动生活建立了联系.用户走到哪里都带着移动设备,可以说移动设备与用户的个人生活(从日历到个人相册)休戚相关.在这种移动生活中,通讯录占据着重要的位置.应用可以使用联系人数据库确定用户是否有朋友注册了相关服务:分析联系人的电子邮件地址或电话号码,并将联系人自动添加为好友.应用还可以使用联系人列表自动填写邮件地址或电话号码,或让用户通过蓝牙与朋友分享联系人信息.应用需要访问用户通讯录的原因不胜枚举. 注意: 除非有充分的理由,否则不要访问

IOS-系统API调用联系人信息

.h文件 //系统自带的联系人framework #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> 加委托 ABPeoplePickerNavigationControllerDelegate { UITextField * aPhoneField;//患者手机号Field } /* picker 系统内置的联系人view对象 @property (nonatomic)ABPeop