IOS 获取系统通讯录中的联系人信息
Posted 郭晓倩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS 获取系统通讯录中的联系人信息相关的知识,希望对你有一定的参考价值。
- (IBAction)getAllContactFromSystem {
ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(ab, ^(bool granted, CFErrorRef error) {
//取得通讯录访问授权
ABAuthorizationStatus authorization= ABAddressBookGetAuthorizationStatus();
if (authorization!=kABAuthorizationStatusAuthorized) {
NSLog(@"尚未获得通讯录访问授权!");
return ;
}
//取得通讯录中所有人员记录
CFArrayRef allPeople= ABAddressBookCopyArrayOfAllPeople(ab);
for (int i=0; i<CFArrayGetCount(allPeople); ++i) {
ABRecordRef recordRef = CFArrayGetValueAtIndex(allPeople, i);
//获取用户名
NSString *firstName = (__bridge NSString *) ABRecordCopyValue(recordRef, kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString *)ABRecordCopyValue(recordRef, kABPersonLastNameProperty);
NSString *personName = [NSString stringWithFormat:@"%@%@",lastName,firstName];
//获取手机号
NSMutableArray *phoneNumbers = [NSMutableArray new];
ABMultiValueRef phoneNumbersRef = ABRecordCopyValue(recordRef, kABPersonPhoneProperty);
for(int j=0; j<ABMultiValueGetCount(phoneNumbersRef); ++j){
NSString* phoneNumber = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phoneNumbersRef, j));
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
if (phoneNumber.length > 0) {
[phoneNumbers addObject:phoneNumber];
}
}
}
//释放资源
CFRelease(allPeople);
});
}
以上是关于IOS 获取系统通讯录中的联系人信息的主要内容,如果未能解决你的问题,请参考以下文章