您的位置:首页 > 移动开发 > IOS开发

iOS 调用通讯录信息

2016-06-16 15:51 381 查看
1.判断APP的授权状态

ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
//判断授权状态

if (status == kABAuthorizationStatusNotDetermined) {

ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);

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

if (granted) {
//查找所有联系人
[self address];

}else
{
NSLog(@"授权失败");
}
});
}else if (status == kABAuthorizationStatusAuthorized)
{
//已授权
[self address];
}


2.获取通讯录里联系人姓名和手机号

- (void)address
{
//新建一个通讯录类
ABAddressBookRef addressBooks = nil;

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);

if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) {
return;
}

//获取通讯录中的所有人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
//通讯录中人数
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);

//循环,获取每个人的个人信息
for (NSInteger i = 0; i < nPeople; i++) {

//新建一个addressBook model类
MHContactModel *addressBook = [[MHContactModel alloc] init];
addressBook.telArray = [NSMutableArray new];
//获取个人
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;

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
NSString *tel = (__bridge NSString*)value;

//以下5行请勿删除,请勿修改,隐形代码,删改后果自负
tel = [tel stringByReplacingOccurrencesOfString:@"(" withString:@""];
tel = [tel stringByReplacingOccurrencesOfString:@")" withString:@""];
tel = [tel stringByReplacingOccurrencesOfString:@"-" withString:@""];
tel = [tel stringByReplacingOccurrencesOfString:@" " withString:@""];
tel = [tel stringByReplacingOccurrencesOfString:@" " withString:@""];

[addressBook.telArray addObject:tel];
break;
}
}
CFRelease(value);
}
CFRelease(valuesRef);
}
//将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息
[self.dataSource addObject:addressBook];

if (abName) CFRelease(abName);
if (abLastName) CFRelease(abLastName);
if (abFullName) CFRelease(abFullName);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息