IOS-系统API调用联系人信息
2013-05-24 08:42
585 查看
.h文件
.m文件
#import <UIKit/UIKit.h> //系统自带的联系人framework #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> @interface EXABViewController : UIViewController<ABPeoplePickerNavigationControllerDelegate> @end
.m文件
// // EXABViewController.m // ExerciseAddressBook // // Created by hxl on 13-5-22. // Copyright (c) 2013年 xiaolei.hu. All rights reserved. // // Revision History // 2013-05-24 | hxl | First draft. #import "EXABViewController.h" @interface EXABViewController () /* picker 系统内置的联系人view对象 @property (nonatomic)ABPeoplePickerNavigationController *picker Discussion 此对象来自于引入的AddressBookUI.framework,系统内置的联系人view对象 */ @property (nonatomic)ABPeoplePickerNavigationController *picker; /* uib UIBUTTON对象 @property (nonatomic)IBOutlet UIButton* uib Discussion 本view中的button按钮 */ @property (nonatomic)IBOutlet UIButton* uib;//在xib中关联button /* btnClick: 点击按钮弹出联系人列表 - (IBAction)btnClick:(id)sender//在xib中关联button的点击事件 Parameters sender 触发当前事件的UIBUTTON控件 Discussion 本方法当点击按钮时会调用系统内置API获取联系人列表 */ - (IBAction)btnClick:(UIButton*)sender; @end @implementation EXABViewController @synthesize picker; @synthesize uib; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)btnClick:(UIButton*)sender { //设置delegate及presentViewController方法在viewDidAppear或者viewWillAppear中调用下面有效,在viewDidLoad中无效 if(!self.picker){ self.picker = [[ABPeoplePickerNavigationController alloc] init]; // place the delegate of the picker to the controll self.picker.modalPresentationStyle = UIModalPresentationCurrentContext; self.picker.modalInPopover = YES; self.picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; self.picker.peoplePickerDelegate = self; // self is the 2nd viewController in the original navigation stack } //显示一个viewcontroller [self presentViewController:self.picker animated:YES completion:nil]; //关闭viewcontroller //- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion ABAddressBookRef abr = [picker addressBook]; if(abr) { //取出所有联系人信息 CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(abr); if (people){ UInt16 numberOfPersonsInAB = CFArrayGetCount(people); //复制CF数组对象 //CFArrayRef cpeople = CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(people), people); ABRecordRef person = nil; CFStringRef firstName = nil; for (UInt16 i = 0; i < numberOfPersonsInAB; i++) { person = CFArrayGetValueAtIndex(people, i); firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); NSLog(@"%@", firstName); } //CFRelease不能释放nil的对象,会crash if(person) { CFRelease(person); } if(firstName) { CFRelease(firstName); } if(people) { CFRelease(people); } } } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //无效果 //self.picker.peoplePickerDelegate = self; //[self presentViewController:self.picker animated:YES completion:nil]; NSLog(@"- (void)viewWillAppear:(BOOL) animated = %d", animated); } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; //可运行 //self.picker.peoplePickerDelegate = self; //[self presentViewController:self.picker animated:YES completion:nil]; NSLog(@"- (void)viewWillLayoutSubviews"); } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; //报错 //self.picker.peoplePickerDelegate = self; //[self presentViewController:self.picker animated:YES completion:nil]; NSLog(@"- (void)viewDidLayoutSubviews"); } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //可运行 //self.picker.peoplePickerDelegate = self; //[self presentViewController:self.picker animated:YES completion:nil]; NSLog(@"- (void)viewDidAppear:(BOOL) animated = %d", animated); } - (void)viewWillDisappear:(BOOL)animated { NSLog(@"- (void)viewWillDisappear:(BOOL) animated = %d", animated); } -(void)viewDidDisappear:(BOOL)animated { NSLog(@"- (void)viewDidDisappear:(BOOL) animated = %d", animated); } /* Discussion 该方法在用户选择通讯录一级列表的某一项时被调用,通过person可以获得选中联系人的所有信息,但当选中的联系人有多个号码,而我们又希望用户可以明确的指定一个号码时(如拨打电话),返回YES允许通讯录进入联系人详情界面: */ - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { CFStringRef firstName, lastName; firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); UIAlertView *myAlertView; myAlertView = [[UIAlertView alloc]initWithTitle:@"你选中了:" message:[NSString stringWithFormat:@"%@.%@", lastName, firstName] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; //点击后弹出该对话框。 [myAlertView show]; [peoplePicker dismissViewControllerAnimated:YES completion:nil]; //虽然使用了ARC模式,但是Core Foundation框架 (CoreFoundation.framework) PS:CF开头的任然需要手动控制内存(CFRELESE) CFRelease(firstName); CFRelease(lastName); return YES; } /* Discussion 当用户进入单个联系人信息(二级页面)点击某个字段时,会调用如下方法,返回YES继续进入下一步,点击NO不进入下一步,比如点击电话,返回YES就拨打电话,返回NO不会拨打电话: */ - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { if (property == kABPersonPhoneProperty) { ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, property); int index = ABMultiValueGetIndexForIdentifier(phoneMulti,identifier); NSString* ns = [NSString stringWithFormat:@"%@",ABMultiValueCopyValueAtIndex(phoneMulti, index)]; UIAlertView *myAlertView; myAlertView = [[UIAlertView alloc]initWithTitle:@"你选中了:" message:ns delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; //点击后弹出该对话框。 [myAlertView show]; [peoplePicker dismissViewControllerAnimated:YES completion:nil]; } return NO; } /* Discussion 当用户离开单个联系人信息(二级页面)点击某个字段时调用 */ - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { [peoplePicker dismissViewControllerAnimated:YES completion:nil]; } @end
相关文章推荐
- IOS调用系统联系人界面获取联系人信息
- iOS 调用系统通讯录 获取联系人信息
- iOS 系统应用调用: 电话、短信息、邮件
- Android学习之调用系统联系人页面返回联系人信息
- iOS 获取系统通讯录联系人信息
- iOS 获取联系人,并调用系统地址簿UI
- Android 调用系统相机拍照(适配7.0)、从图库选择图片、从数据库读取联系人信息、相册图片、扫描文件
- 调用系统应用选择联系人并返回联系人信息
- IOS获取系统通讯录联系人信息
- iOS 调用系统相关功能(本机信息,手电筒,电话,短信,通讯录,内存,电池,亮度)
- ios开发之 -- 调用系统定位获取当前经纬度与地理信息
- iOS调用电话本保存联系人信息或者打电话发短信不跳转
- IOS 调用系统发邮件Api
- iOS - (利用/调用系统定位获取当前经纬度与地理信息)
- iOS - (利用/调用系统定位获取当前经纬度与地理信息)
- IOS获取系统通讯录联系人信息
- 调用API获取系统信息
- iOS调用系统的通讯录界面获取用户信息
- IOS 获取系统通讯录中的联系人信息
- IOS获取系统通讯录联系人信息