IOS-系统API调用联系人信息
来源:互联网 发布:mac如何进入屏保模式 编辑:程序博客网 时间:2024/06/05 06:02
.h文件
#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-系统API调用联系人信息
- iOS 调用系统通讯录 获取联系人信息
- IOS调用系统联系人界面获取联系人信息
- 调用系统应用选择联系人并返回联系人信息
- Android学习之调用系统联系人页面返回联系人信息
- IOS获取系统通讯录联系人信息
- IOS获取系统通讯录联系人信息
- IOS获取系统通讯录联系人信息
- IOS获取系统通讯录联系人信息
- iOS 获取系统通讯录联系人信息
- android调用系统通讯录程序,获取联系人信息
- 通过ContentResolver调用系统URI实现的读取联系人信息
- ios获取联系人信息
- 获取系统联系人信息
- 获取系统联系人信息
- 获取系统联系人信息
- 获取系统联系人信息
- 调用系统联系人
- 内网IP地址\私有IP地址
- Hibernate学习笔记
- 解压jffs2文件——mount jffs2文件到一个文件夹内
- 395. The darkest hour is nearest dawn. 黎明前的黑暗
- MFC debug的时候可以用代码关闭线程 但是编译出来直接执行不可以
- IOS-系统API调用联系人信息
- 软件绿化之实践一--绿化福昕PDF阅读器
- 木匠打造“全木”手表 仅一根弹簧为金属
- 浅谈JAVA中多态
- const在函数声明中的应用
- 字符串处理-研发积累细节
- 从PPS被百度收购看PPTV为何“皇帝的女儿不愁嫁”?
- SQL NOT IN NULL的问题
- Qt应用程序运行