联系人获取
// // 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