标签:ios 调取本地通讯录
这里就粗滤的进行一些简要的介绍,工程需要自己创建,只要既然如相应的下面的语句就可以
首先我们在工程中要添加一个库 AddressBook.framework
之后我们要创建一个Model类:
这里的类名用小写了,懒得改了,大家使用时一定要改过来
#import <Foundation/Foundation.h>
@interface model : NSObject
@property (nonatomic,assign)NSInteger sectionNumber;
@property (nonatomic,assign)NSInteger recordID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *tel;
@end
在具体的controllerView中进行代码书写如下:
创建一个TXLTableViewController类
#import "TXLTableViewController.h"
#import <AddressBook/AddressBook.h>
#import "model.h"
@interface TXLTableViewController ()
@property (nonatomic,strong)NSMutableArray *addressBookTemp;
@end
//NSMutableArray *addressBookTemp;
@implementation TXLTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
//初始化
self.addressBookTemp = [NSMutableArray array];
//新建一个通讯录
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);
}
else
{
addressBooks = ABAddressBookCreate();
}
//获取通讯录中的所有人 ABAddressBookCopyArrayOfAllPeople 返回值是 CFArrayRef 类型的
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
//获取通讯录中的所有联系人的个数
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
//循环,获取每个人的个人信息
for (NSInteger i = 0; i < nPeople; i++)
{
//新建一个addressBook model类
model *address = [[model alloc] init];
//获取个人
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//获取个人名字 Objective-C 对象和 Core Foundation 对象之间互相转换时,使用__bridge
CFTypeRef Name = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *nameString = (__bridge NSString *)Name;
//获取个人电话
ABMultiValueRef tmlphone = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* telphone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(tmlphone, 0);
address.tel =telphone;
address.name = nameString;
//读取通讯录中联系人的唯一标识
address.recordID = (int)ABRecordGetRecordID(person);
// //将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息
[self.addressBookTemp addObject:address];
}
}
本文出自 “ios” 博客,请务必保留此出处http://10136044.blog.51cto.com/10126044/1691389
标签:ios 调取本地通讯录
原文地址:http://10136044.blog.51cto.com/10126044/1691389