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


目录
相关文章
|
3月前
|
搜索推荐 Android开发 iOS开发
探索安卓与iOS系统的用户界面设计哲学
现代移动操作系统的设计哲学不仅仅是技术的表现,更是用户体验与功能实现的结合。本文将深入分析安卓与iOS两大主流系统在用户界面设计方面的差异与共通之处,探讨它们背后的思维模式及其对用户体验的影响。 【7月更文挑战第11天】
|
4月前
|
存储 安全 编译器
我给 iOS 系统打了个补丁——修复 iOS 16 系统键盘重大 Crash
我给 iOS 系统打了个补丁——修复 iOS 16 系统键盘重大 Crash
127 2
我给 iOS 系统打了个补丁——修复 iOS 16 系统键盘重大 Crash
|
16天前
|
开发工具 Swift 数据安全/隐私保护
移动应用开发之旅:从零到一的iOS系统探索
【9月更文挑战第16天】在数字时代的浪潮中,移动应用成为连接用户与数字世界的桥梁。本文将带你走进iOS移动操作系统的世界,了解其架构、设计理念以及开发环境。我们将通过Swift语言的简单示例,展示如何构建一个基本的iOS应用,并探讨移动应用开发的未来趋势。无论你是编程新手还是资深开发者,这篇文章都将为你提供宝贵的见解和知识。
|
9天前
|
监控 Android开发 iOS开发
深入探索安卓与iOS的系统架构差异:理解两大移动平台的技术根基在移动技术日新月异的今天,安卓和iOS作为市场上最为流行的两个操作系统,各自拥有独特的技术特性和庞大的用户基础。本文将深入探讨这两个平台的系统架构差异,揭示它们如何支撑起各自的生态系统,并影响着全球数亿用户的使用体验。
本文通过对比分析安卓和iOS的系统架构,揭示了这两个平台在设计理念、安全性、用户体验和技术生态上的根本区别。不同于常规的技术综述,本文以深入浅出的方式,带领读者理解这些差异是如何影响应用开发、用户选择和市场趋势的。通过梳理历史脉络和未来展望,本文旨在为开发者、用户以及行业分析师提供有价值的见解,帮助大家更好地把握移动技术发展的脉络。
|
2月前
|
存储 安全 编译器
我给 iOS 系统打了个补丁——修复 iOS 16 系统键盘重大 Crash
我给 iOS 系统打了个补丁——修复 iOS 16 系统键盘重大 Crash
我给 iOS 系统打了个补丁——修复 iOS 16 系统键盘重大 Crash
|
2月前
|
人工智能 自然语言处理 云计算
iOS迎来AI升级:揭秘Apple全新“智能”系统
iOS迎来AI升级:揭秘Apple全新“智能”系统
iOS迎来AI升级:揭秘Apple全新“智能”系统
|
2月前
|
iOS开发 开发者
iOS 16 系统键盘修复问题之汇编层面模拟两次返回操作的实现如何解决
iOS 16 系统键盘修复问题之汇编层面模拟两次返回操作的实现如何解决
|
2月前
|
存储 iOS开发
iOS 16 系统键盘修复问题之确定UIKeyboardTaskQueue类对_lock的加锁和解锁操作如何解决
iOS 16 系统键盘修复问题之确定UIKeyboardTaskQueue类对_lock的加锁和解锁操作如何解决
|
2月前
|
安全 Android开发 iOS开发
安卓与iOS的终极对决:哪个系统更适合你?
在智能手机的世界里,安卓和iOS两大操作系统如同两座巍峨的山峰,各自拥有庞大的用户群体。本文将深入浅出地探讨这两个系统的优缺点,并帮助你找到最适合自己的那一款。让我们一起揭开这场技术盛宴的序幕吧!
|
2月前
|
编译器 C语言 iOS开发
iOS 16 系统键盘修复问题之确定_lock是否用于保护对_deferredTasks的多线程读写如何解决
iOS 16 系统键盘修复问题之确定_lock是否用于保护对_deferredTasks的多线程读写如何解决