IOS中获取本地通讯录联系人以及汉字首字母排序
程序员文章站
2024-02-19 08:36:00
ios中获取手机通讯录中的联系人信息:
/*** 加载本地联系人*/
- (void)loadlocalcontacts
{
//新建一个通讯...
ios中获取手机通讯录中的联系人信息:
/*** 加载本地联系人*/ - (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的代理方法也贴一下:
- (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。
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得类别方法
- (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; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!