//获取通讯录
-(void)GetUserAddressBook
{
//获取通讯录权限
ABAddressBookRef ab = NULL;
// ABAddressBookCreateWithOptions is iOS 6 and up.
if (&ABAddressBookCreateWithOptions) {
CFErrorRef error = nil;
ab = ABAddressBookCreateWithOptions(NULL, &error);
if (error) { NSLog(@"%@", error); }
}
if (ab == NULL) {
ab = ABAddressBookCreate();
}
if (ab) {
// ABAddressBookRequestAccessWithCompletion is iOS 6 and up.适配IOS6以上版本
if (&ABAddressBookRequestAccessWithCompletion) {
ABAddressBookRequestAccessWithCompletion(ab, ^(bool granted, CFErrorRef error) {
if (granted) {
// constructInThread: will CFRelease ab.
[NSThread detachNewThreadSelector:@selector(constructInThread:)toTarget:self withObject:CFBridgingRelease(ab)];} else {
//CFRelease(ab);
// Ignore the error
}
});
} else {
// constructInThread: will CFRelease ab.
[NSThread detachNewThreadSelector:@selector(constructInThread:)toTarget:self withObject:CFBridgingRelease(ab)];
}
}
}
//获取到addressbook的权限
-(void)constructInThread:(ABAddressBookRef) ab
{
NSMutableArray *addressBook = [[NSMutableArray alloc]init];
CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(ab);
NSMutableArray* contactArray = [[NSMutableArray alloc]init];
for(int i = 0; i < CFArrayGetCount(results); i++)
{
ABRecordRef person = CFArrayGetValueAtIndex(results, i);
//姓
NSString *firstName = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
//姓音标
//NSString *firstNamePhonetic = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty));
//名
NSString *lastname = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
////名音标
//NSString *lastnamePhonetic = (NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty));
if (firstName ==NULL) {
firstName = @"";
}
if (lastname == NULL) {
lastname = @"";
}
NSString *name = [lastname stringByAppendingString:firstName];
NSString * personPhone;
//读取电话多值
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int k = 0; k
{
//获取电话Label
NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
//获取該Label下的电话值
personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);
if (personPhone !=NULL) {
break;
}
}
if (personPhone == NULL) {
personPhone = @"";
}
NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:name,@"name",personPhone,@"phone",@"0",@"selected", nil];
if (name.length && personPhone.length) {
[_addressBookArr addObject:dic];
}
}
NSLog(@"********%@",_addressBookArr);
}