-(IBAction)getContact
{
if (self.infoArray.count > 0) {
return;
}
ABAddressBookRef addressBook=nil;
CGFloat systemVersion = [[UIDevice currentDevice].systemVersion floatValue];
if (systemVersion >= 6.0)//系统版本大约等于6
{
addressBook=ABAddressBookCreateWithOptions(NULL, NULL);
dispatch_semaphore_t sema=dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
// dispatch_release(sema);
}
else//系统版本小于6
{
addressBook=ABAddressBookCreate();
}
ABAddressBookRef tmpAddressBook = ABAddressBookCreate();
CFArrayRef tmpPeoples = ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(tmpAddressBook);
double interval = [[NSDatedate] timeIntervalSince1970];
NSLog(@"interval start:%f", interval);
for (int kk = 0; kk < 1; kk++) {
for (int i=0; i<nPeople; i++) {
NSMutableArray * phoneArray=nil;
NSString *contactName = nil;
ABRecordRef person = CFArrayGetValueAtIndex(tmpPeoples, i);
//获取的联系人单一属性:First name
NSString * tmpFirstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
//获取的联系人单一属性:Last name
NSString* tmpLastName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
//获取的联系人单一属性:Generic phone number
ABMultiValueRef tmpPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++)
{
NSString* tmpPhoneIndex = (__bridge NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j);
tmpPhoneIndex = [tmpPhoneIndex stringByReplacingOccurrencesOfString:@"+86" withString:@""];
tmpPhoneIndex = [tmpPhoneIndex stringByReplacingOccurrencesOfString:@" " withString:@""];
tmpPhoneIndex = [tmpPhoneIndex stringByReplacingOccurrencesOfString:@"-" withString:@""];
if (tmpPhoneIndex.length != 11) {
continue;
}
if (!phoneArray)
{
phoneArray=[[NSMutableArray alloc] initWithObjects:tmpPhoneIndex, nil];
}
else
{
[phoneArray addObject:tmpPhoneIndex];
}
}
//这个联系人的正确手机号码至少得有一个
if (phoneArray.count > 0) {
if (tmpFirstName.length!=0 && ![tmpFirstName isKindOfClass:[NSNull class]])//姓长度不为0也不为NUll
{
if (tmpLastName.length==0||[tmpLastName isKindOfClass:[NSNull class]])//名长度为0或是null
{
contactName=[NSString stringWithFormat:@"%@",tmpFirstName];
}
else//名长度既不为零,名也不为null
{
contactName=[NSString stringWithFormat:@"%@%@",tmpLastName,tmpFirstName];
}
}
else//姓长度为0或为null
{
if (tmpLastName.length!=0&&![tmpLastName isKindOfClass:[NSNull class]])//名长度不为0也不为null
{
contactName=[NSString stringWithFormat:@"%@",tmpLastName];
}
}
if (self.infoArray.count == 0) {
self.infoArray = [[NSMutableArray alloc] init];
}
for (int j = 0; j < phoneArray.count; j++) {
NSString* phone = [phoneArray objectAtIndex:j];
ContactsUserInfo* info = [[ContactsUserInfo alloc] init];
info.tel = phone;
info.name = contactName;
[self.infoArray addObject:info];
}
}
}
}
[self.mTableViewreloadData];
}