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

.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


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值