- (void)viewDidLoad { [super viewDidLoad]; self.addressBookArray =[[NSMutableArray alloc] init]; [self getAddressBookArray]; // 得到手机通讯录信息 }
#pragma mark - 获取系统通讯录 - (void)getAddressBookArray { // 新建一个通讯录 ABAddressBookRef addressBooks = nil; if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)// 判断当前设备的系统版本,因为iOS6之后获取通讯录的方式有变化 { addressBooks = ABAddressBookCreateWithOptions(NULL, NULL); // 获取通讯录权限 dispatch_semaphore_t sema = dispatch_semaphore_create(0); // 参考http://blog.csdn.net/robincui2011/article/details/9270249 ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error) { dispatch_semaphore_signal(sema); }); // 请求访问的用户地址薄数据 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); // 等待一个信号量
}else { addressBooks = ABAddressBookCreateWithOptions(NULL, NULL); } // 获取通讯录中所有的人 CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(addressBooks); // 通讯录中的人数 CFIndex nperson = ABAddressBookGetPersonCount(addressBooks); // 循环,获取每个人的个人信息 for (NSInteger i = 0; i < nperson; i++) { // 新建一个addressBook Model类 AddressBookModel *addressBook = [[AddressBookModel alloc] init]; // 获取个人 ABRecordRef person = CFArrayGetValueAtIndex(allPerson, 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; addressBook.recordID = (int)ABRecordGetRecordID(person); ABPropertyID multiProperties[] = { kABPersonPhoneProperty, kABPersonEmailProperty }; NSInteger multiPropertiesTotal = sizeof(multiProperties)/sizeof(ABPropertyID); // ??? for (NSInteger j = 0; j < multiPropertiesTotal; j++) { ABPropertyID property = multiProperties[j]; ABMultiValueRef valueRef = ABRecordCopyValue(person, property); // 性质 NSInteger valuesCount = 0; // 什么用法?获取参考值得个数 if (valueRef != nil) valuesCount = ABMultiValueGetCount(valueRef); // 参考ref if (valuesCount == 0) { CFRelease(valueRef); continue; } // 获取电话号码和email for (NSInteger k = 0; k < valuesCount; k++) { CFTypeRef value = ABMultiValueCopyValueAtIndex(valueRef, k); switch (j) { case 0: // phone number { addressBook.tel = (__bridge NSString *)value; break; } case 1: // Email { addressBook.email = (__bridge NSString *)value; break; }
default: break; } CFRelease(value); } CFRelease(valueRef); // 干什么用的? } // 将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息 [self.addressBookArray addObject:addressBook]; if (abName) CFRelease(abName); if (abLastName) CFRelease(abLastName); if (abFullName) CFRelease(abFullName);
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return self.addressBookArray.count; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addressBookCell" forIndexPath:indexPath];
AddressBookModel * bookModel = [self.addressBookArray objectAtIndex:indexPath.row]; cell.textLabel.text = bookModel.name; cell.detailTextLabel.text = bookModel.tel; return cell; } -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%s",__FUNCTION__); // 拨打电话 NSString *tel = ((AddressBookModel *)[self.addressBookArray objectAtIndex:indexPath.row]).tel;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",tel]]];
// 给对应号码发短信 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"sms:// %@",tel]]];
}
|
|