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
时间: 2024-08-04 13:57:14

ios调用系统通讯录的相关文章

android调用系统通讯录号码全显示

问题描述 android调用系统通讯录号码全显示 各位大牛,android调用系统通讯录时如何让号码全显示出来?默认是只显示姓名的,如图1,怎么弄成图2的效果,淘宝.京东等的手机充值选择号码都用图2的界面,而且和系统通讯录风格一致,应该不是自定义的通讯录. 解决方案 android调用系统通讯录android 内容提供者 系统通讯录iOS开发调用系统通讯录获取电话号码

两种iOS调用系统发短信的方法_IOS

一.程序外调用系统发短信 这个方法其实很简单,直接调用openURL即可: NSURL *url = [NSURL URLWithString:@"sms://15888888888"]; [[UIApplication sharedApplication]openURL:url]; 二.程序内调用系统发短信 这种方法有一个好处就是用户发短信之后还可以回到App. 首先要导入MessageUI.framework,并引入头文件: #import <MessageUI/Messag

iOS 调用系统相机和相册的方法

#import "CameraViewController.h" @interface CameraViewController () @end @implementation CameraViewController – (void)viewDidLoad { [super viewDidLoad]; NSArray *arr = @[@"调用相机",@"调用图片库",@"打开闪关灯",@"关闭闪光灯"]

Android调用系统相机拍照保存以及调用系统相册的方法

系统已经有的东西,如果我们没有新的需求的话,直接调用是最直接的.下面讲讲调用系统相机拍照并保 存图片和如何调用系统相册的方法. 首先看看调用系统相机的核心方法: Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(camera, 100); 相机返回的数据通过下面的回调方法取得,并处理 @Override protected void onActivityResult(int re

Android中调用系统的文件浏览器及自制简单的文件浏览器_Android

调用系统自带的文件浏览器这很简单: /** 调用文件选择软件来选择文件 **/ private void showFileChooser() { intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); try { startActivityForResult(Intent.createChooser(inten

Android 调用系统照相机拍照和录像_Android

本文实现android系统照相机的调用来拍照 项目的布局相当简单,只有一个Button: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heig

IOS中调用系统拨打电话与发送短信

IOS中调用系统拨打电话发送短信 一.调用打电话界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",_phoneNumber]]]; 二.发送短消息界面 调用系统的发送短信的界面,需要引入以下头文件: #import <MessageUI/MessageUI.h> 系统短信界面的调用很简单,只需下面几句代码: ? 1

ios可以调用系统自带的日历吗

问题描述 ios可以调用系统自带的日历吗 我想在自己的应用中打开系统的日历应用,该怎么弄,比如可以打开相机等可以打开日历吗? 解决方案 现在的 App Store 里有很多课程表的 Apps,比如「课程格子」,当然也有著名的「iStudiz Pro」,这些 Apps 共同特点都是能很好的管理课程表和作业列表,还能提醒你做作业或者是考试.然而,通常我们并没有那么高的需求,没有必要单独安装一款课表 App. 因此,我们完全可以利用 iOS 自带的「日历」App,配合「通知中心」的便利,来完成这一简单

IOS 设置系统音量 (已实践,设置时会弹出系统的音量显示,调用很方便)

IOS 设置系统音量 分类: IOS Object-C 2012-11-08 11:47 389人阅读 评论(0)收藏 举报 很简单的调用  首先在工程引入MediaPlayer.framework 在调节音量的地方加入头文件 [cpp] view plaincopy #import <MediaPlayer/MediaPlayer.h>   两行代码搞定~ [cpp] view plaincopy MPMusicPlayerController *mp=[MPMusicPlayerContr