iOS7 iOS8 调用系统通讯录界面

#pragma mark--选取手机联系人

-(void)selectPeople{

//这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录

int
__block tip=0;

//声明一个通讯簿的引用

ABAddressBookRef addBook =nil;

//创建通讯簿的引用

addBook=ABAddressBookCreateWithOptions(NULL,
NULL);

//创建一个出事信号量为0的信号

dispatch_semaphore_t sema=dispatch_semaphore_create(0);

//申请访问权限

ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted,
CFErrorRef error)        {

//greanted为YES是表示用户允许,否则为不允许

if (!greanted) {

tip=1;

}

//发送一次信号

dispatch_semaphore_signal(sema);

});

//等待信号触发

dispatch_semaphore_wait(sema,
DISPATCH_TIME_FOREVER);

if (tip) {

//做一个友好的提示

UIAlertView * alart = [[UIAlertView
alloc]initWithTitle:@""
message:@"此应用程序没有权限访问您的联系人。您可以在‘设置‘->‘隐私‘->‘通讯录‘->‘健一网‘中启用访问权限"
delegate:self
cancelButtonTitle:@"知道了"
otherButtonTitles:nil,
nil];

[alart show];

}else{

_peoplePicker= [[ABPeoplePickerNavigationController
alloc] init];

_peoplePicker.peoplePickerDelegate =
self;

NSArray *displayItems = [NSArray
arrayWithObjects:[NSNumber
numberWithInt:kABPersonPhoneProperty],nil];

_peoplePicker.displayedProperties=displayItems;

//        if(IOS8){

//            _peoplePicker.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];

//        }

[self
presentViewController:_peoplePicker
animated:YES
completion:nil];

}

}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate

//ios8执行

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {

ABMultiValueRef valuesRef =
ABRecordCopyValue(person,
kABPersonPhoneProperty);

//查找这条记录中的名字

NSString *firstName =
CFBridgingRelease(ABRecordCopyValue(person,
kABPersonFirstNameProperty));

firstName = firstName != nil? firstName:@"";

//查找这条记录中的姓氏

NSString *lastName =
CFBridgingRelease(ABRecordCopyValue(person,
kABPersonLastNameProperty));

lastName = lastName != nil? lastName:@"";

NSLog(@"%@",[NSString
stringWithFormat:@"%@%@",firstName,lastName]);

//     CFRelease(person);

CFIndex index =
ABMultiValueGetIndexForIdentifier(valuesRef,identifier);

if(property !=
kABPersonPhoneProperty) {

return;

}else{

CFStringRef value =
ABMultiValueCopyValueAtIndex(valuesRef,index);

[self
dismissViewControllerAnimated:YES
completion:^{

selectPeople=[NSString
stringWithFormat:@"%@%@",lastName,firstName];

selectNumber=[NSString
stringWithFormat:@"%@",(__bridge
NSString*)value];

if ([selectNumber
isEqualToString:@"(null)"]) {

selectNumber=@"";

}else{

selectNumber=[selectNumber
stringByReplacingOccurrencesOfString:@"-"
withString:@""];

selectNumber=[HYShareObject
formatPhoneNum:selectNumber];

}

[self.table
reloadData];

}];

}

}

//ios7执行

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
NS_DEPRECATED_IOS(2_0,
8_0){

if(property !=
kABPersonPhoneProperty) {

return
NO;

}else{

ABMultiValueRef valuesRef =
ABRecordCopyValue(person,
kABPersonPhoneProperty);

//查找这条记录中的名字

NSString *firstName =
CFBridgingRelease(ABRecordCopyValue(person,
kABPersonFirstNameProperty));

firstName = firstName != nil? firstName:@"";

//查找这条记录中的姓氏

NSString *lastName =
CFBridgingRelease(ABRecordCopyValue(person,
kABPersonLastNameProperty));

lastName = lastName != nil? lastName:@"";

NSLog(@"%@",[NSString
stringWithFormat:@"%@%@",firstName,lastName]);

//     CFRelease(person);

CFIndex index =
ABMultiValueGetIndexForIdentifier(valuesRef,identifier);

CFStringRef value =
ABMultiValueCopyValueAtIndex(valuesRef,index);

[self
dismissViewControllerAnimated:YES
completion:^{

selectPeople=[NSString
stringWithFormat:@"%@%@",lastName,firstName];

selectNumber=[NSString
stringWithFormat:@"%@",(__bridge
NSString*)value];

if ([selectNumber
isEqualToString:@"(null)"]) {

selectNumber=@"";

}else{

selectNumber=[selectNumber
stringByReplacingOccurrencesOfString:@"-"
withString:@""];

selectNumber=[HYShareObject
formatPhoneNum:selectNumber];

}

[self.table
reloadData];

}];

}

return
YES;

}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

{

[self
dismissViewControllerAnimated:YES
completion:nil];

}

时间: 2024-08-06 21:27:01

iOS7 iOS8 调用系统通讯录界面的相关文章

iOS开发--调用系统通讯录界面

今天写代码遇到了要调用系统通讯录,看了一些博客发现写的都是获取通讯录的内容,而不是调用系统的界面. 分享一下自己写的代码 第一步:引入 #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> 第二步:添加点击事件 创建一个通讯录界面 并以present的方式跳转 #pragma mark -- IBAction - (IBAction)buttonClicked:(id)sender {

iOS --调用系统通讯录

// 调用系统通讯录需要遵循两个代理ABPeoplePickerNavigationControllerDelegate,UINavigationControllerDelegate 相关类为ABPeoplePickerNavigationController // 系统通讯录自带导航栏,所有要model出来 // 初始化 ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController

怎么调用系统通讯录并向被选中联系人发送短信

每做一个项目都会有收获,前提是要在这个项目上付出努力的! 好吧,如今讲一下:怎么通过调用系统通讯录,当你点击联系人姓名时,跳转到向其发送短信的页面<收件人是被点中的联系人,短信已自己主动编辑>. 看看图片效果: 以下看一下具体代码: Uri result = data.getData(); String phoneName = getPhoneContacts(result); Log.d("phone", "---------->phoneName==&q

Android 调用系统通讯录

这个过程有三步:1)许可向你的应用程序的manifest中添加读取通讯录数据的许可  <uses-permission android:name="android.permission.READ_CONTACTS" />    <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 2)调用Contact Picker在你的activity中,创建一个Inten

调用系统通讯录+条件查询联系人

#import "ZYViewController.h"#import "ZYPeople.h"#import "ChineseToPinyin.h"//第三方库 @interface ZYViewController () @end @implementation ZYViewController - (void)viewDidLoad{    [super viewDidLoad];        _tableView.delegate =s

iOS -------- 应用程序引用系统通讯录

转自:http://www.cnblogs.com/ygm900/p/3472288.html 由于ios系统对用户隐私的控制,第三方应用程序只能通过苹果官方接口调用系统通讯录,不能像android那样直接操作通讯录数据库.     一般地,使用系统自带通讯录的方法有两种,一种是直接将整个通讯录引入到应用程序,另一种是逐条读取通讯录中的每一条联系人信息.下面我们就一一详解. 1 直接引用整个通讯录 使用的类:ABPeoplePickerNavigationController方法: 在Local

IOS问题汇总:2015-1-9 iOS 调用系统发短信以及打电话功能

iOS 调用系统发短信以及打电话功能 ios电话smsinterface互联网class先介绍一种最简单的方法: 调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@“tel://10086”]]; 调用发短信功能 [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@“sms://10000”]]; 上面的发短信的功能是调用系统的

IOS 调用系统发短信以及打电话的功能

IOS 调用系统发短信以及打电话的功能 http://blog.csdn.net/lwq421336220/article/details/7818979 先介绍一种最简单的方法: 调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]]; 调用发短信功能 [[UIApplication sharedApplication]openURL:[NSURL URLWithS

Android编程,通过ContentResolver调用系统URI实现的通讯录示例

Android的四大组件之一,ContentProvider,分为两个部分:一是数据组织工具ContentProvider,一是数据调用工具ContentResolver.对于系统已经提供的URI,如通讯录.多媒体.短信等,通常可以不需要再建立额外的数据库,再用ContentProvider进行组织,而是可以直接用ContentResolver调用这些URI,对系统数据库进行增删改查等操作,从而保证整个Android设备中数据的统一. 下面这个链接是本人前段时间为了参加MM论坛的一个小比赛所做的