自从出了ios8之后, 使用这个的时候,我也遇到了一些麻烦,于是再次修改一次版本。
//
// HYBContactHelper.h
// XiaoYaoUser
//
// Created by 黄仪标 on 14/12/9.
// Copyright (c) 2014年 xiaoyaor. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
/*!
* 操作本地通讯录API封装
*/
@interface HYBContactHelper : NSObject <ABPeoplePickerNavigationControllerDelegate> {
@private
__weak UIViewController *_targetController;
HYBSuccessDictBlock _completionBlock;
ABPeoplePickerNavigationController *_pickerView;
}
- (void)showInController:(UIViewController *)controller completion:(HYBSuccessDictBlock)completion;
@end
//
// HYBContactHelper.m
// XiaoYaoUser
//
// Created by 黄仪标 on 14/12/9.
// Copyright (c) 2014年 xiaoyaor. All rights reserved.
//
#import "HYBContactHelper.h"
@implementation HYBContactHelper
- (void)showInController:(UIViewController *)controller completion:(HYBSuccessDictBlock)completion {
_completionBlock = [completion copy];
_targetController = controller;
_pickerView = [[ABPeoplePickerNavigationController alloc] init];
_pickerView.peoplePickerDelegate = self;
_pickerView.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
[_targetController presentViewController:_pickerView animated:YES completion:nil];
}
#pragma mark - ABPeoplePickerNavigationControllerDelegate
// Called after a property has been selected by the user.
// 8.0之后才会调用
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
if (firstName==nil) {
firstName = @" ";
}
NSString *lastName=(__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
if (lastName==nil) {
lastName = @" ";
}
NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
for (int i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {
NSString *aPhone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMulti, i);
[phones addObject:aPhone];
}
NSString *phone = @"";
if (phones.count > 0) {
phone = [phones objectAtIndex:0];
}
NSDictionary *dic = @{@"fullname": [NSString stringWithFormat:@"%@%@", firstName, lastName]
,@"phone" : phone};
_completionBlock(dic);
[_pickerView dismissViewControllerAnimated:YES completion:nil];
}
// Called after the user has pressed cancel.
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
_completionBlock(nil);
[_pickerView dismissViewControllerAnimated:YES completion:nil];
}
// 8.0之前才会调用
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
return YES;
}
// 8。0之前才会调用
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier {
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
if (firstName==nil) {
firstName = @" ";
}
NSString *lastName=(__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
if (lastName==nil) {
lastName = @" ";
}
NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
for (int i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {
NSString *aPhone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMulti, i);
[phones addObject:aPhone];
}
NSString *phone = @"";
if (phones.count > 0) {
phone = [phones objectAtIndex:0];
}
NSDictionary *dic = @{@"fullname": [NSString stringWithFormat:@"%@%@", firstName, lastName]
,@"phone" : phone};
_completionBlock(dic);
[_pickerView dismissViewControllerAnimated:YES completion:nil];
// 不执行默认的操作,如,打电话
return NO;
}
@end