如题 这是项目中遇到的问题
具体情况是这样 首先获取手机的通讯录 通过TableView进行显示出来姓名、电话 核心代码如下
//获取读取电话本权限
- (void)accessTheAddress{
ABAddressBookRef addressBook = nil;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
//等待同意后向下执行
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (!granted) {
[self gotoSetting];
return ;
}
dispatch_semaphore_signal(sema);
// NSLog(@"这里是用户选择是否允许后的执行代码");
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex num = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *arr = [NSMutableArray array];//保存获取的数据
NSMutableArray *gloubArr = [NSMutableArray array];//数据进行分类
for (NSInteger i = 0; i < num; i++) {
ABRecordRef people = CFArrayGetValueAtIndex(allLinkPeople, i);
ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);
for (int k = 0; k<ABMultiValueGetCount(phones); k++)
{
NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, k);
//获取联系人详细信息,如:姓名,电话,住址等信息
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(people, kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString *)ABRecordCopyValue(people, kABPersonLastNameProperty);
NSString *personName;
if (firstName == NULL) {
personName = [NSString stringWithFormat:@"%@",lastName];
}else if (lastName == NULL){
personName = [NSString stringWithFormat:@"%@",firstName];
}else if(firstName == NULL && lastName == NULL){
personName = @"";
}else{
personName = [NSString stringWithFormat:@"%@%@",firstName,lastName];
}
NSString *newP = [personPhone stringByReplacingOccurrencesOfString:@" " withString:@""];
newP = [newP stringByReplacingOccurrencesOfString:@"-" withString:@""];
// 去除+86
if ([newP containsString:@"+86"]) {
newP = [newP stringByReplacingOccurrencesOfString:@"+86" withString:@""];
}
// 去除+
if ([newP containsString:@"+"]) {
newP = [newP stringByReplacingOccurrencesOfString:@"+" withString:@""];
}
Contact *model = [[Contact alloc]init];
model.phone = newP;
model.name = personName;
model.userCode = [UserDanli shareMultUserInfo].userCode;
model.type = @"0";
[arr addObject:model];
}
}
for (int i=0; i<arr.count; i++) {
Contact *contact = arr[i];
NSString *sectionName = [[NSString stringWithFormat:@"%c",pinyinFirstLetter([contact.name characterAtIndex:0])] uppercaseString];
NSUInteger firstLetter = [ALPHA rangeOfString:[sectionName substringToIndex:1]].location;
if (firstLetter != NSNotFound)
{
contact.sortLetters = sectionName;
contact.firstLetter = [NSString stringWithFormat:@"%lu", (unsigned long)firstLetter];
}
[gloubArr addObject:contact];
}
[_database clearListPhoneContact];//先清空表 再插入
[_database insertTotalDatabaseWithArray:arr];
dataArray = [NSMutableArray arrayWithArray: [_database findAll]];
//block里面刷新UI 最好回到主线程刷新 否则容易crash
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showError:@"同步完成"];
[self changeGroup];
});
});
}
else{
}
}
//去设置页面
- (void)gotoSetting{
NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];
NSString *message = [NSString stringWithFormat:@"请在%@的\"设置-隐私-通讯录\"选项中,\r允许%@访问你的通讯录。",[UIDevice currentDevice].model,appName];
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertVC addAction:cancleAction];
[alertVC addAction:sureAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
注:这两端代码中间有一小部分 是百度看来的
TableView加载的内容 大致是 上面姓名 下面号码 右边有两个图片 分别是短信图片 和电话图片 点击 可发短信 打电话
于是问题来了 点击时 是需要获取该TableView cell上的姓名和电话的 分段设置tag 势在必行 代码如下
static NSString *CellIdentifier = @"Cell";
ContactCell *cell = (ContactCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ContactCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.delegate = self;
}
cell.tag = indexPath.section*20000+indexPath.row+20000;
如何使用呢
因为我在ColltactCell这个cell中设置了两个代理方法 代理传tag 两个图片给tag分别设置为indexPath.row indexPath.row+5000
下面就是发短信 打电话的代码了
#pragma ContactCell Delegate
- (void)getMessageImageVTag:(NSInteger)tag{
NSInteger section = (tag-20000)/20000;
NSInteger row = tag-section*20000-20000;
//发短信
MFMessageComposeViewController *mf = [[MFMessageComposeViewController alloc]init];
if ([MFMessageComposeViewController canSendText]) {
mf.body = @"楼下拿快递";
mf.recipients = [NSArray arrayWithObjects:[[phoneArray objectAtIndex:section] objectAtIndex:row], nil];
mf.messageComposeDelegate = self;
[self presentViewController:mf animated:YES completion:^{}];
}
}
- (void)getTelImageVTag:(NSInteger)tag{
NSInteger section = (tag-20000-5000)/20000;
NSInteger row = tag-section*20000-20000-5000;
NSString *urlString = @"";
if (searchbar.text.length) {
urlString = ((Contact *)[filteredArray objectAtIndex:row]).phone;
}else{
urlString = [[phoneArray objectAtIndex:section] objectAtIndex:row];
}
//打电话
UIWebView* callWebview =[[UIWebView alloc] init];
NSURL *telURL =[NSURL URLWithString:urlString];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
[self.view addSubview:callWebview];
}
最后附上我的一张截图