码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 获取系统通讯录中的联系人信息

时间:2016-03-24 11:29:08      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

- (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 获取系统通讯录中的联系人信息

标签:

原文地址:http://www.cnblogs.com/guoxiaoqian/p/5314734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!