服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - IOS中获取本地通讯录联系人以及汉字首字母排序

IOS中获取本地通讯录联系人以及汉字首字母排序

2021-02-24 15:33u011619283 IOS

这篇文章主要介绍了IOS中获取本地通讯录联系人以及汉字首字母排序的相关资料,需要的朋友可以参考下

iOS中获取手机通讯录中的联系人信息:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*** 加载本地联系人*/
- (void)loadLocalContacts
{
  //新建一个通讯录类
  ABAddressBookRef addressBooks = nil;
   
  if (DeviceVersion < 6.0) {
    addressBooks = ABAddressBookCreate();
  } else {
    addressBooks = ABAddressBookCreateWithOptions(NULL, NULL);
    //获取通讯录权限
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    dispatch_release(sema);
  }
   
  //判断授权状态
  if (ABAddressBookGetAuthorizationStatus()!=kABAuthorizationStatusAuthorized) {
    return ;
  }
   
  //获取通讯录中的所有人
  CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
  //通讯录中人数
  CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
  NSMutableArray *persons = [[NSMutableArray alloc] init];
  for (int i = 0; i < nPeople; i++) {
    //获取个人
    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
    //获取个人名字
    NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    NSMutableString *name = [[NSMutableString alloc] init];
    if (firstName == nil && lastName == nil) {
      NSLog(@"名字不存在的情况");
      name = nil;
    }
    if (lastName) {
      [name appendString:lastName];
    }
    if (firstName) {
      [name appendString:firstName];
    }
     
    ABMultiValueRef tmlphone = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString *telphone = (NSString *)ABMultiValueCopyValueAtIndex(tmlphone, 0);
    if (telphone != nil) {
      telphone = [telphone stringByReplacingOccurrencesOfString:@"-" withString:@""];
      NSString *title = [NSString stringWithFormat:@"%@(%@)",name,telphone];
      [persons addObject:title];
    }
  }
   
  //对联系人进行分组和排序
  UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];
  NSInteger highSection = [[theCollation sectionTitles] count]; //中文环境下返回的应该是27,是a-z和#,其他语言则不同
   
  //_indexArray 是右侧索引的数组,也是secitonHeader的标题
  _indexArray = [[NSMutableArray alloc] initWithArray:[theCollation sectionTitles]];
   
  NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:highSection];
  //初始化27个空数组加入newSectionsArray
  for (NSInteger index = 0; index < highSection; index++) {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [newSectionsArray addObject:array];
    [array release];
  }
   
  for (NSString *p in persons) {
    //获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11
    NSInteger sectionNumber = [theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)];
    //把name为“林丹”的p加入newSectionsArray中的第11个数组中去
    NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
    [sectionNames addObject:p];
  }
   
  for (int i = 0; i < newSectionsArray.count; i++) {
    NSMutableArray *sectionNames = newSectionsArray[i];
    if (sectionNames.count == 0) {
      [newSectionsArray removeObjectAtIndex:i];
      [_indexArray removeObjectAtIndex:i];
      i--;
    }
  }
   
  //_contacts 是联系人数组(确切的说是二维数组)
  self.contacts = newSectionsArray;
  [newSectionsArray release];
   
  [self.tableView reloadData];
}

顺便把索引和tableView dataSource的代理方法也贴一下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return self.contacts.count;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return [self.contacts[section] count];
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *identifier = @"contactCell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  }
   
  cell.imageView.image = [UIImage imageNamed:@"default_head"];
  cell.textLabel.text = [self.contacts objectAtIndex:indexPath.section][indexPath.row];
  return cell;
}
 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  return [_indexArray objectAtIndex:section];
}
 
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
  return _indexArray;
}
 
//索引列点击事件
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
  return index;
}

还有两个很重要的方法:

下面这个方法是[theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)]; 是这里的p对象要实现的方法,我这里的p是NSString,你也可以用其他对象例如Person。

?
1
2
3
4
5
6
7
8
9
10
11
NSString *ret = @"";
  if (![self canBeConvertedToEncoding: NSASCIIStringEncoding]) {//如果是英语
    if ([[self letters] length]>2) {
      ret = [[self letters] substringToIndex:1];
    }
  }
  else {
    ret = [NSString stringWithFormat:@"%c",[self characterAtIndex:0]];
  }
  return ret;
}

下面这个方法是NSString得类别方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (NSString *)letters{
  NSMutableString *letterString = [NSMutableString string];
  int len = [self length];
  for (int i = 0;i < len;i++)
  {
    NSString *oneChar = [[self substringFromIndex:i] substringToIndex:1];
    if (![oneChar canBeConvertedToEncoding:NSASCIIStringEncoding]) {
      NSArray *temA = makePinYin2([oneChar characterAtIndex:0]);
      if ([temA count]>0) {
        oneChar = [temA objectAtIndex:0];
      }
    }
    [letterString appendString:oneChar];
  }
  return letterString;
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/u011619283/article/details/45745955

延伸 · 阅读

精彩推荐
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03