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


目录
相关文章
|
5月前
|
iOS开发
Cisco Catalyst 9800 Wireless Controller, IOS XE Release 17.17.1 ED - 思科无线控制器系统软件
Cisco Catalyst 9800 Wireless Controller, IOS XE Release 17.17.1 ED - 思科无线控制器系统软件
123 9
Cisco Catalyst 9800 Wireless Controller, IOS XE Release 17.17.1 ED - 思科无线控制器系统软件
|
云安全 安全 Cloud Native
Cisco Catalyst 8000 Series IOS XE 17.18.1a ED 发布 - 思科边缘平台系列系统软件
Cisco Catalyst 8000 Series IOS XE 17.18.1a ED - 思科边缘平台系列系统软件
35 0
|
运维 监控 安全
Cisco ISR 4000 Series IOS XE 17.18.1a ED 发布 - 思科 4000 系列集成服务路由器 IOS XE 系统软件
Cisco ISR 4000 Series IOS XE 17.18.1a ED - 思科 4000 系列集成服务路由器 IOS XE 系统软件
30 0
|
人工智能 监控 安全
思科 Catalyst 9000 交换产品系列 IOS XE 系统软件 17.18.1 ED
Cisco Catalyst 9000 Series Switches, IOS XE Release 17.18.1 ED
56 0
|
11月前
|
安全 Android开发 数据安全/隐私保护
深入探讨iOS与Android系统安全性对比分析
在移动操作系统领域,iOS和Android无疑是两大巨头。本文从技术角度出发,对这两个系统的架构、安全机制以及用户隐私保护等方面进行了详细的比较分析。通过深入探讨,我们旨在揭示两个系统在安全性方面的差异,并为用户提供一些实用的安全建议。
|
11月前
|
安全 Android开发 数据安全/隐私保护
深入探索Android与iOS系统安全性的对比分析
在当今数字化时代,移动操作系统的安全已成为用户和开发者共同关注的重点。本文旨在通过比较Android与iOS两大主流操作系统在安全性方面的差异,揭示两者在设计理念、权限管理、应用审核机制等方面的不同之处。我们将探讨这些差异如何影响用户的安全体验以及可能带来的风险。
471 21
|
11月前
|
传感器 iOS开发 UED
探索iOS生态系统:从App Store优化到用户体验提升
本文旨在深入探讨iOS生态系统的多个方面,特别是如何通过App Store优化(ASO)和改进用户体验来提升应用的市场表现。不同于常规摘要仅概述文章内容的方式,我们将直接进入主题,首先介绍ASO的重要性及其对开发者的意义;接着分析当前iOS平台上用户行为的变化趋势以及这些变化如何影响应用程序的设计思路;最后提出几点实用建议帮助开发者更好地适应市场环境,增强自身竞争力。
|
11月前
|
安全 Android开发 iOS开发
深入探讨Android与iOS系统的差异及未来发展趋势
本文旨在深入分析Android和iOS两大移动操作系统的核心技术差异、用户体验以及各自的市场表现,进一步探讨它们在未来技术革新中可能的发展方向。通过对比两者的开放性、安全性、生态系统等方面,本文揭示了两大系统在移动设备市场中的竞争态势和潜在变革。
|
11月前
|
存储 安全 算法
深入探索iOS系统安全机制:保护用户隐私的前沿技术
本文旨在探讨苹果公司在其广受欢迎的iOS操作系统中实施的先进安全措施,这些措施如何共同作用以保护用户的隐私和数据安全。我们将深入了解iOS的安全架构,包括其硬件和软件层面的创新,以及苹果如何通过持续的软件更新来应对新兴的安全威胁。此外,我们还将讨论iOS系统中的一些关键安全功能,如Face ID、加密技术和沙箱环境,以及它们如何帮助防止未经授权的访问和数据泄露。
|
11月前
|
安全 数据安全/隐私保护 Android开发
深入探索iOS系统安全机制:从基础到高级
本文旨在全面解析iOS操作系统的安全特性,从基础的权限管理到高级的加密技术,揭示苹果如何构建一个既开放又安全的移动平台。我们将通过实例和分析,探讨iOS系统如何保护用户数据免受恶意软件、网络攻击的威胁,并对比Android系统在安全性方面的差异。

热门文章

最新文章