ios调用系统通讯录

自己写了一个调用系统通讯录类,可以直接复制来使用!

#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

/*!
 * 操作本地通讯录API封装
 */
@interface HYBContactHelper : NSObject <ABPeoplePickerNavigationControllerDelegate, ABPersonViewControllerDelegate> {
  @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;
  [_targetController presentViewController:_pickerView animated:YES completion:nil];
}

#pragma mark -  ABPeoplePickerNavigationControllerDelegate

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0

// Called after a person has been selected by the user.
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker
                         didSelectPerson:(ABRecordRef)person {
  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];
  }
  NSDictionary *dic = @{@"fullname": [NSString stringWithFormat:@"%@%@", firstName, lastName],
                        @"phone" : phones.count > 0 ? [phones firstObject] : @"读取失败"};
  
  _completionBlock(dic);
  [_targetController dismissViewControllerAnimated:YES completion:nil];
  return;
}

// Called after a property has been selected by the user.
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker
                         didSelectPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier {
  _completionBlock(nil);
  [_targetController dismissViewControllerAnimated:YES completion:nil];
  return;
}

#else

// Called after the user has pressed cancel.
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
  _completionBlock(nil);
  [_targetController dismissViewControllerAnimated:YES completion:nil];
}


// Deprecated, use predicateForSelectionOfPerson and/or -peoplePickerNavigationController:didSelectPerson: instead.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person {
  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];
  }
  NSDictionary *dic = @{@"firstName": firstName,@"lastName":lastName,@"phones":phones};
  
  _completionBlock(dic);
  [_targetController dismissViewControllerAnimated:YES completion:nil];
  return NO;
}

// Deprecated, use predicateForSelectionOfProperty and/or -peoplePickerNavigationController:didSelectPerson:property:identifier: instead.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier {
  _completionBlock(nil);
  [_targetController dismissViewControllerAnimated:YES completion:nil];
  return NO;
}
#endif

#pragma mark - ABPersonViewControllerDelegate
// Called when the user selects an individual value in the Person view, identifier will be kABMultiValueInvalidIdentifier if a single value property was selected.
// Return NO if you do not want anything to be done or if you are handling the actions yourself.
// Return YES if you want the ABPersonViewController to perform its default action.
- (BOOL)personViewController:(ABPersonViewController *)personViewController
shouldPerformDefaultActionForPerson:(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];
  }
  NSDictionary *dic = @{@"firstName": firstName,@"lastName":lastName,@"phones":phones};
  
  _completionBlock(dic);
  [_targetController dismissViewControllerAnimated:YES completion:nil];
  return NO;
}

@end


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值