1.加入 AddressBook库

推断授权状态

-(bool)checkAddressBookAuthorizationStatus

{

//取得授权状态

ABAuthorizationStatus authStatus =

ABAddressBookGetAuthorizationStatus();

if (authStatus !=kABAuthorizationStatusAuthorized)

{

returnNO;

}else{

returnYES;

}

}

注冊 通讯录变更通知

-(void)createChangeCallBack{

CFErrorRef error =NULL;

myAddressBook =ABAddressBookCreateWithOptions(NULL,
&error);

}

//移除通知

- (void)unregisterCallback {

ABAddressBookUnregisterExternalChangeCallback(myAddressBook,ContactsChangeCallback,nil);

}

收到变更通知后回调

void ContactsChangeCallback (ABAddressBookRef addressBook,

CFDictionaryRef info,

void *context){

),
^{

ABAddressBookUnregisterExternalChangeCallback(addressBook,ContactsChangeCallback,nil);

});

}

- (IBAction)add:(id)sender {

ABAddressBookRequestAccessWithCompletion(ABAddressBookRef addressBookRef,
^(bool granted,CFErrorRef error) {

if (granted) {

dispatch_async(dispatch_get_main_queue(), ^{

[self getContactsFromAddressBook];

});

}else {

// TODO: Show alert

}

});

}

-(void)getContactsFromAddressBook

{

CFErrorRef error =NULL;

ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, &error);

ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted,CFErrorRef
error) {

if (granted) {

NSArray *allContacts = (__bridge_transferNSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);

NSMutableArray *mutableContacts = [NSMutableArrayarrayWithCapacity:allContacts.count];

;

; i<[allContactscount]; i++)

{

THContact *contact = [[THContactalloc]init];//封装通讯录的model

ABRecordRef contactPerson = (__bridgeABRecordRef)allContacts[i];

contact.recordId =ABRecordGetRecordID(contactPerson);

// Get first and last names

NSString *firstName = (__bridge_transferNSString*)ABRecordCopyValue(contactPerson,kABPersonFirstNameProperty);

NSString *lastName = (__bridge_transferNSString*)ABRecordCopyValue(contactPerson,kABPersonLastNameProperty);

NSString * midName = (__bridge_transferNSString*)ABRecordCopyValue(contactPerson,kABPersonMiddleNameProperty);

// Set Contact properties

contact.firstName = firstName;

contact.lastName = lastName;

contact.middleName = midName;

contact.name = [contactfullName];

// Get mobile number

ABMultiValueRef phonesRef =ABRecordCopyValue(contactPerson,
kABPersonPhoneProperty);

contact.phone = [selfgetMobilePhoneProperty:phonesRef];

if(phonesRef) {

CFRelease(phonesRef);

}

)
{

[mutableContactsaddObject:contact];

}

}

if(addressBook) {

CFRelease(addressBook);

}

//处理获取通讯后的逻辑

}

});

}

- (NSMutableArray *)getMobilePhoneProperty:(ABMultiValueRef)phonesRef

{

NSMutableArray * array = [NSMutableArrayarray];

; k<ABMultiValueGetCount(phonesRef);
k++)

{

//获取电话Label

//        NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phonesRef, k));

//获取該Label下的电话值

NSString * personPhone = (__bridgeNSString*)ABMultiValueCopyValueAtIndex(phonesRef,
k);

if (personPhone) {

[arrayaddObject:personPhone];

}

}

return array;

}

通讯录中联系人相关的应用iPhone提供了两个框架:AddressBook.framework和AddressBookUI.framework,使用这两个框架我们能够在程序中訪问并显示iPhone数据库中的联系人信息。



1.AddressBookUI显示部分



AddressBookUI中提供了和联系人显示信息相关的一些Controller,有四个:



ABPeoplePickerNavigationController:显示整个通讯录并能够选择一个联系人的信息



ABPersonViewController:显示一个详细联系人的信息



ABNewPersonViewController:添加一个新的联系人



ABUnknownPersonViewController:完好一个联系人的信息



因为当中最基本的是ABPeoplePickerNavigationController。因此就详细的介绍一下通过程序显示整个通讯录而且能够选择当中某个联系人信息的步骤。

(a)创建并初始化一个ABPeoplePickerNavigationController对象



(b)设置其代理(delegate)



(c)用presentModalViewController:animated:这种方法进行显示整个通讯录页面



样例: 项目需求。一个lable,text是一个电话,把这个电话号 加入到通讯录中得莫一个人。

#import <AddressBookUI/ABNewPersonViewController.h>

#import <AddressBookUI/ABPeoplePickerNavigationController.h>

@property (strong,nonatomic)ABPeoplePickerNavigationController
*picker;

@property (strong,nonatomic)ABNewPersonViewController
* pickerPerson;

self.picker = [[ABPeoplePickerNavigationController
alloc]
init];

_picker.peoplePickerDelegate 
= self;

self.pickerPerson = [[ABNewPersonViewController
alloc]
init];

_pickerPerson.newPersonViewDelegate  =
self;

//先推出 联系人列表

-(void)editContactItemBtn:(id)editItem{

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

}

//实现代理,在点击联系人列表的时候,创建一个ABRecordRef。传给加入联系人列表

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person{

ABRecordRef contactPerson = person;

NSString *firstName = (__bridge_transfer
NSString*)ABRecordCopyValue(contactPerson,
kABPersonFirstNameProperty);

NSString *lastName = (__bridge_transfer
NSString*)ABRecordCopyValue(contactPerson,
kABPersonLastNameProperty);

NSString * midName = (__bridge_transfer
NSString*)ABRecordCopyValue(contactPerson,
kABPersonMiddleNameProperty);

ABMultiValueRef phonesRef =
ABRecordCopyValue(contactPerson,
kABPersonPhoneProperty);

NSMutableArray * phones = [self
getMobilePhoneProperty:phonesRef];

if(phonesRef) {

CFRelease(phonesRef);

}

ABRecordRef newperson =
ABPersonCreate();

ABRecordSetValue(newperson,
kABPersonFirstNameProperty, CFBridgingRetain(firstName),
NULL);

ABRecordSetValue(newperson,
kABPersonMiddleNameProperty,
CFBridgingRetain(midName), NULL);

ABRecordSetValue(newperson,
kABPersonLastNameProperty, CFBridgingRetain(lastName),
NULL);

NSString * phone =
@"13212345678";

NSString * label =
@"其它";

NSDictionary * dic = [NSDictionary
dictionaryWithObjectsAndKeys:phone,@"phone",label,@"lable",
nil];

[phones addObject:dic];

ABMutableMultiValueRef mulRef =
ABMultiValueCreateMutable(kABMultiStringPropertyType);

for(int i =
; i < phones.count; i++){

NSDictionary * tempDic = [phones
objectAtIndex:i];

NSString * tempPhone = [tempDic
objectForKey:@"phone"];

NSString * templable = [tempDic
objectForKey:@"lable"];

ABMultiValueIdentifier multivalueIdentifier;

ABMultiValueAddValueAndLabel(mulRef, (__bridge
CFStringRef)tempPhone, (__bridge
CFStringRef)templable, &multivalueIdentifier);

}

ABRecordSetValue(newperson,
kABPersonPhoneProperty, mulRef, NULL);

if(mulRef)

CFRelease(mulRef);

_pickerPerson.displayedPerson =newperson;

[self
dismissViewControllerAnimated:YES
completion:nil];//先把当前的miss掉,然后再推出下个

UINavigationController * nav = [[UINavigationController
alloc]initWithRootViewController:_pickerPerson];

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

}

//加入联系人页面,不用区分是取消还是完毕,系统的功能。不用自己写了

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person{

[self
dismissViewControllerAnimated:YES
completion:nil];

}

- (NSMutableArray *)getMobilePhoneProperty:(ABMultiValueRef)phonesRef

{

NSMutableArray * array = [NSMutableArray
array];

for (int k =
; k<ABMultiValueGetCount(phonesRef); k++)

{

//获取电话Label

NSString * personPhoneLabel = (__bridge
NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phonesRef, k));

//获取該Label下的电话值

NSString * personPhone = (__bridge
NSString*)ABMultiValueCopyValueAtIndex(phonesRef, k);

if (personPhone) {

NSDictionary * dic = [NSDictionary
dictionaryWithObjectsAndKeys:personPhone,@"phone",personPhoneLabel,@"lable",
nil];

[array addObject:dic];

}

}

return array;

}



版权声明:本文博客原创文章,博客,未经同意,不得转载。

iOS 获取联系人,并调用系统地址簿UI的更多相关文章

  1. iOS 获取设备信息,mac地址,IP地址,设备名称

    #import "DeviceInfoUtil.h" #import "GlobleData.h" #import "sys/utsname.h&qu ...

  2. iOS中 读取相册,调用系统相机 技术分享

    技术内容:分别读取相册以及调取相机,将图片显示到imageView上 布局: 1.创建imageView 和 button 并为button一个关联pickerImage的事件 <div sty ...

  3. esp32使iOS 获取蓝牙外设的Mac地址

    最近在做一个需要上下位机的项目,我负责的任务下位机,使用的主控芯片是esp32.这个项目中有一项是需要手机扫描二维码然后连接作为esp32的蓝牙.二维码中包含了mac地址信息,在手机扫描周围设备的时候 ...

  4. iOS - (个人隐私钱包调用系统本机TouchID指纹锁验证)

    // //  ViewController.m //  TouchID指纹验证 // //  Created by apple on 16/9/18. //  Copyright © 2016年 ap ...

  5. iOS 获取设备型号以及IP地址

    首先导入四个头文件 #include <sys/types.h> #include <sys/sysctl.h> #include <ifaddrs.h> #inc ...

  6. iOS 获取手机型号,系统版本

    新添加判断iPhone 7.iPhone 7 Plus ,我手里没有7,判断不对表打我~ FQ找的资料:http://www.iphonehacks.com/download-iphone-ios-f ...

  7. ios openURL的使用(调用系统电话、浏览器、地图、邮件等)

    Safari Any URL starting with http:// which does not point to maps.google.com or www.youtube.com is s ...

  8. ios获取本机网络IP地址方法

    #include <ifaddrs.h> #include <arpa/inet.h>     - (NSString *)getIPAddress {           N ...

  9. iOS 获取WIFI SSID及MAC地址

    NSString *ssid = @"Not Found"; NSString *macIp = @"Not Found"; CFArrayRef myArra ...

随机推荐

  1. [C#基础] 继承

    虚方法和覆写方法 虚方法可以使基类的引用访问"升至"派生类中 可以使用基类引用调用派生类的方法,只需满足下面的条件 派生类的方法和基类的方法有相同的签名和返回类型 基类的方法使用v ...

  2. 使用Jquery+EasyUI项目开发情况的框架是中评---员工管理源代码共享

    使用Jquery+EasyUI 进行框架项目开发案例解说之中的一个 员工管理源代码分享 在開始解说之前,我们先来看一下什么是Jquery EasyUI?jQuery EasyUI是一组基于jQuery ...

  3. 在安装mysql出现的错误以及解决方法

    因为手贱更新了一下驱动,结果导致无线网卡出了问题.然而就算是从官网上下载了驱动各种折腾也没有弄好,心里特别堵.无奈只有重装系统这一条路了.这里表示特别难过,因为电脑上东西实在太多了,而且各种环境变量. ...

  4. 计算机视觉与模式识别代码合集第二版one

    Topic Name Reference code Feature Detection, Feature Extraction, and Action Recognition Space-Time I ...

  5. Android输入法扩展之外接键盘中文输入

    大家想不想要这样一台Android  Surface平板,看着就过瘾吧. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSVRsZWFrcw==/font/ ...

  6. cocos2dx游戏开发学习笔记3-lua面向对象分析

    在lua中,能够通过元表来实现类.对象.继承等.与元表相关的方法有setmetatable().__index.getmetatable().__newindex. 详细什么是元表在这里就不细说了,网 ...

  7. dedecms 文章列表和频道列表同时调用

    演示效果:http://www.mypf110.com/qcd/ <div class="changshi_wrap"> {dede:channelartlist ro ...

  8. ORA-00210 ORA-15001 ORA-15055 ORA-01031: insufficient privileges

    ORA-00210: cannot open the specified control file ORA-00202: control file: &apos;+DATA/posdb/con ...

  9. 50 tips of JavaScript

    50 tips of JavaScript,这些坑你都知道吗? 1.在局部作用域中,使用var操作符定义的变量将成为定义该变量的作用域中的局部变量,省略var的会创建全局变量:在全局作用域中,不管是否 ...

  10. jQuery中的getJSON()

    json文件是一种轻量级的数据交互格式.一般在jQuery中使用getJSON()方法读取. $.getJSON(url,[data],[callback]) url:json文件地址 data:可选 ...