ios获取手机通讯录php,IOS_iOS开发中使app获取本机通讯录的实现代码实例,一、在工程中添加AddressBook.fram - phpStudy...

iOS开发中使app获取本机通讯录的实现代码实例

一、在工程中添加AddressBook.framework和AddressBookUI.framework

二、获取通讯录

1、在infterface中定义数组并在init方法中初始化

NSMutableArray *addressBookTemp;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

addressBookTemp = [NSMutableArray array];

}

2、定义一个model,用来存放通讯录中的各个属性

新建一个继承自NSObject的类,在.h中

@interface TKAddressBook : NSObject {

NSInteger sectionNumber;

NSInteger recordID;

NSString *name;

NSString *email;

NSString *tel;

}

@property NSInteger sectionNumber;

@property NSInteger recordID;

@property (nonatomic, retain) NSString *name;

@property (nonatomic, retain) NSString *email;

@property (nonatomic, retain) NSString *tel;

@end

在.m文件中进行synthesize

@implementation TKAddressBook

@synthesize name, email, tel, recordID, sectionNumber;

@end

3、获取联系人

在iOS6之后,获取通讯录需要获得权限

//新建一个通讯录类

ABAddressBookRef addressBooks = nil;

if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)

{

addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);

//获取通讯录权限

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

dispatch_release(sema);

}

else

{

addressBooks = ABAddressBookCreate();

}

//获取通讯录中的所有人

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);

//通讯录中人数

CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);

//循环,获取每个人的个人信息

for (NSInteger i = 0; i < nPeople; i++)

{

//新建一个addressBook model类

TKAddressBook *addressBook = [[TKAddressBook alloc] init];

//获取个人

ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

//获取个人名字

CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);

CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);

CFStringRef abFullName = ABRecordCopyCompositeName(person);

NSString *nameString = (__bridge NSString *)abName;

NSString *lastNameString = (__bridge NSString *)abLastName;

if ((__bridge id)abFullName != nil) {

nameString = (__bridge NSString *)abFullName;

} else {

if ((__bridge id)abLastName != nil)

{

nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];

}

}

addressBook.name = nameString;

addressBook.recordID = (int)ABRecordGetRecordID(person);;

ABPropertyID multiProperties[] = {

kABPersonPhoneProperty,

kABPersonEmailProperty

};

NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);

for (NSInteger j = 0; j < multiPropertiesTotal; j++) {

ABPropertyID property = multiProperties[j];

ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);

NSInteger valuesCount = 0;

if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);

if (valuesCount == 0) {

CFRelease(valuesRef);

continue;

}

//获取电话号码和email

for (NSInteger k = 0; k < valuesCount; k++) {

CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);

switch (j) {

case 0: {// Phone number

addressBook.tel = (__bridge NSString*)value;

break;

}

case 1: {// Email

addressBook.email = (__bridge NSString*)value;

break;

}

}

CFRelease(value);

}

CFRelease(valuesRef);

}

//将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息

[addressBookTemp addObject:addressBook];

if (abName) CFRelease(abName);

if (abLastName) CFRelease(abLastName);

if (abFullName) CFRelease(abFullName);

}

三、显示在table中

//行数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;

}

//列数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [addressBookTemp count];

}

//cell内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *cellIdentifier = @"ContactCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil){

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

}

TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row];

cell.textLabel.text = book.name;

cell.detailTextLabel.text = book.tel;

return cell;

}

列表效果

PS:通讯录中电话号码中的"-"可以在存入数组之前进行处理,属于NSString处理的范畴,解决办法有很多种,本文不多加说明

四、删除通讯录数据

    ABRecordRef person = objc_unretainedPointer([myContacts objectAtIndex:indexPath.row]);

CFErrorRef *error;

ABAddressBookRemoveRecord(addressBook, person, error);

ABAddressBookSave(addressBook, error);

myContacts = nil;

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];相关阅读:

php 判断服务器操作系统的类型

JSP上传文件到指定位置实例代码

C++多重继承与虚继承分析

AngularJS入门教程之静态模板详解

winform 使用Anchor属性进行界面布局的方法详解

win10正式版应用商店更新修复bug 增加对Groove Pass的支持

基于jQuery的图片不完全按比例自动缩小

一张表搞清楚php is_null、empty、isset的区别

一个命令禁止恢复Win10硬盘已删文件的技巧

MySQL性能优化之max_connections配置参数浅析

错误代码10107 win7上不了网解决办法

Android Mouse实现过程详细笔记

PHP实现的线索二叉树及二叉树遍历方法详解

浅谈java中Map的用法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值