IOS调用系统联系人界面获取联系人信息

.h文件

  1. #import <UIKit/UIKit.h>  
  2. //系统自带的联系人framework  
  3. #import <AddressBook/AddressBook.h>  
  4. #import <AddressBookUI/AddressBookUI.h>  
  5.   
  6. @interface EXABViewController : UIViewController<ABPeoplePickerNavigationControllerDelegate>  
  7.   
  8. @end  

.m文件


  1. //  
  2. //  EXABViewController.m  
  3. //  ExerciseAddressBook  
  4. //  
  5. //  Created by hxl on 13-5-22.  
  6. //  Copyright (c) 2013年 xiaolei.hu. All rights reserved.  
  7. //  
  8. //  Revision History  
  9. //  2013-05-24 | hxl | First draft.  
  10.   
  11. #import "EXABViewController.h"  
  12.   
  13. @interface EXABViewController ()  
  14.   
  15. /* 
  16.  picker 
  17.  系统内置的联系人view对象 
  18.   
  19.  @property (nonatomic)ABPeoplePickerNavigationController *picker 
  20.   
  21.  Discussion 
  22.  此对象来自于引入的AddressBookUI.framework,系统内置的联系人view对象 
  23.  */  
  24. @property (nonatomic)ABPeoplePickerNavigationController *picker;  
  25.   
  26.   
  27. /* 
  28.  uib 
  29.  UIBUTTON对象 
  30.   
  31.  @property (nonatomic)IBOutlet UIButton* uib 
  32.   
  33.  Discussion 
  34.  本view中的button按钮 
  35.  */  
  36. @property (nonatomic)IBOutlet UIButton* uib;<span style="font-family: Arial, Helvetica, sans-serif;">//在xib中关联button</span>  
  37.   
  38.   
  39.   
  40. /* 
  41.  btnClick: 
  42.  点击按钮弹出联系人列表 
  43.   
  44.  - (IBAction)btnClick:(id)sender//在xib中关联button的点击事件 
  45.   
  46.  Parameters 
  47.  sender 
  48.  触发当前事件的UIBUTTON控件 
  49.   
  50.  Discussion 
  51.  本方法当点击按钮时会调用系统内置API获取联系人列表 
  52.  */  
  53. - (IBAction)btnClick:(UIButton*)sender;  
  54.   
  55. @end  
  56.   
  57. @implementation EXABViewController  
  58.   
  59. @synthesize picker;  
  60.   
  61. @synthesize uib;  
  62.   
  63. - (void)viewDidLoad  
  64. {  
  65.     [super viewDidLoad];  
  66.     // Do any additional setup after loading the view, typically from a nib.  
  67.       
  68. }  
  69.   
  70. - (void)didReceiveMemoryWarning  
  71. {  
  72.     [super didReceiveMemoryWarning];  
  73.     // Dispose of any resources that can be recreated.  
  74. }  
  75.   
  76. - (IBAction)btnClick:(UIButton*)sender  
  77. {  
  78.     //设置delegate及presentViewController方法在viewDidAppear或者viewWillAppear中调用下面有效,在viewDidLoad中无效  
  79.     if(!self.picker){  
  80.         self.picker = [[ABPeoplePickerNavigationController alloc] init];  
  81.         // place the delegate of the picker to the controll  
  82.         self.picker.modalPresentationStyle = UIModalPresentationCurrentContext;  
  83.         self.picker.modalInPopover = YES;  
  84.         self.picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;  
  85.         self.picker.peoplePickerDelegate = self;  
  86.         // self is the 2nd viewController in the original navigation stack  
  87.     }  
  88.       
  89.     //显示一个viewcontroller  
  90.     [self presentViewController:self.picker animated:YES completion:nil];  
  91.       
  92.     //关闭viewcontroller  
  93.     //- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion  
  94.       
  95.     ABAddressBookRef abr = [picker addressBook];  
  96.     if(abr) {  
  97.         //取出所有联系人信息  
  98.         CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(abr);  
  99.         if (people){  
  100.             UInt16 numberOfPersonsInAB = CFArrayGetCount(people);  
  101.             //复制CF数组对象  
  102.             //CFArrayRef cpeople = CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(people), people);  
  103.               
  104.             ABRecordRef person = nil;  
  105.             CFStringRef firstName = nil;  
  106.             for (UInt16 i = 0; i < numberOfPersonsInAB; i++) {  
  107.                 person = CFArrayGetValueAtIndex(people, i);  
  108.                 firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);  
  109.                 NSLog(@"%@", firstName);  
  110.             }  
  111.             //CFRelease不能释放nil的对象,会crash  
  112.             if(person) {  
  113.                 CFRelease(person);  
  114.             }  
  115.             if(firstName) {  
  116.                 CFRelease(firstName);  
  117.             }  
  118.             if(people) {  
  119.                 CFRelease(people);  
  120.             }  
  121.               
  122.         }  
  123.     }  
  124. }  
  125.   
  126. - (void)viewWillAppear:(BOOL)animated  
  127. {  
  128.     [super viewWillAppear:animated];  
  129.     //无效果  
  130.     //self.picker.peoplePickerDelegate = self;  
  131.     //[self presentViewController:self.picker animated:YES completion:nil];  
  132.     NSLog(@"- (void)viewWillAppear:(BOOL) animated = %d", animated);  
  133. }  
  134.   
  135. - (void)viewWillLayoutSubviews  
  136. {  
  137.     [super viewWillLayoutSubviews];  
  138.     //可运行  
  139.     //self.picker.peoplePickerDelegate = self;  
  140.     //[self presentViewController:self.picker animated:YES completion:nil];  
  141.     NSLog(@"- (void)viewWillLayoutSubviews");  
  142. }  
  143.   
  144. - (void)viewDidLayoutSubviews  
  145. {  
  146.     [super viewDidLayoutSubviews];  
  147.     //报错  
  148.     //self.picker.peoplePickerDelegate = self;  
  149.     //[self presentViewController:self.picker animated:YES completion:nil];  
  150.     NSLog(@"- (void)viewDidLayoutSubviews");  
  151. }  
  152.   
  153. -(void)viewDidAppear:(BOOL)animated  
  154. {  
  155.     [super viewDidAppear:animated];  
  156.     //可运行  
  157.     //self.picker.peoplePickerDelegate = self;  
  158.     //[self presentViewController:self.picker animated:YES completion:nil];  
  159.     NSLog(@"- (void)viewDidAppear:(BOOL) animated = %d", animated);  
  160. }  
  161.   
  162. - (void)viewWillDisappear:(BOOL)animated  
  163. {  
  164.     NSLog(@"- (void)viewWillDisappear:(BOOL) animated = %d", animated);  
  165. }  
  166.   
  167. -(void)viewDidDisappear:(BOOL)animated  
  168. {  
  169.     NSLog(@"- (void)viewDidDisappear:(BOOL) animated = %d", animated);  
  170. }  
  171.   
  172. /* 
  173.  Discussion 
  174.  该方法在用户选择通讯录一级列表的某一项时被调用,通过person可以获得选中联系人的所有信息,但当选中的联系人有多个号码,而我们又希望用户可以明确的指定一个号码时(如拨打电话),返回YES允许通讯录进入联系人详情界面: 
  175.  */  
  176. - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker  
  177.       shouldContinueAfterSelectingPerson:(ABRecordRef)person  
  178. {  
  179.       
  180.     CFStringRef firstName, lastName;  
  181.     firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);  
  182.     lastName  = ABRecordCopyValue(person, kABPersonLastNameProperty);  
  183.       
  184.     UIAlertView *myAlertView;  
  185.     myAlertView = [[UIAlertView alloc]initWithTitle:@"你选中了:" message:[NSString stringWithFormat:@"%@.%@", lastName, firstName] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];  
  186.     //点击后弹出该对话框。  
  187.     [myAlertView show];  
  188.     [peoplePicker dismissViewControllerAnimated:YES completion:nil];  
  189.     //虽然使用了ARC模式,但是Core Foundation框架 (CoreFoundation.framework) PS:CF开头的任然需要手动控制内存(CFRELESE)  
  190.     CFRelease(firstName);  
  191.     CFRelease(lastName);  
  192.     return YES;  
  193. }  
  194.   
  195. /* 
  196.  Discussion 
  197.  当用户进入单个联系人信息(二级页面)点击某个字段时,会调用如下方法,返回YES继续进入下一步,点击NO不进入下一步,比如点击电话,返回YES就拨打电话,返回NO不会拨打电话: 
  198.  */  
  199. - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker  
  200.       shouldContinueAfterSelectingPerson:(ABRecordRef)person  
  201.                                 property:(ABPropertyID)property  
  202.                               identifier:(ABMultiValueIdentifier)identifier  
  203. {  
  204.       
  205.     if (property == kABPersonPhoneProperty) {  
  206.         ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, property);  
  207.           
  208.         int index = ABMultiValueGetIndexForIdentifier(phoneMulti,identifier);  
  209.           
  210.         NSString* ns = [NSString stringWithFormat:@"%@",ABMultiValueCopyValueAtIndex(phoneMulti, index)];  
  211.           
  212.         UIAlertView *myAlertView;  
  213.         myAlertView = [[UIAlertView alloc]initWithTitle:@"你选中了:" message:ns delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];  
  214.         //点击后弹出该对话框。  
  215.         [myAlertView show];  
  216.         [peoplePicker dismissViewControllerAnimated:YES completion:nil];  
  217.           
  218.     }  
  219.       
  220.     return NO;  
  221.       
  222. }  
  223.   
  224. /* 
  225.  Discussion 
  226.  当用户离开单个联系人信息(二级页面)点击某个字段时调用 
  227.  */  
  228. - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker  
  229. {  
  230.     [peoplePicker dismissViewControllerAnimated:YES completion:nil];  
  231. }  
  232. @end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值