ABPeoplePickerNavigationController

自从出了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


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值