IOS中获取本地通讯录联系人以及汉字首字母排序
iOS中获取手机通讯录中的联系人信息:
/***加载本地联系人*/
-(void)loadLocalContacts
{
//新建一个通讯录类
ABAddressBookRefaddressBooks=nil;
if(DeviceVersion<6.0){
addressBooks=ABAddressBookCreate();
}else{
addressBooks=ABAddressBookCreateWithOptions(NULL,NULL);
//获取通讯录权限
dispatch_semaphore_tsema=dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBooks,^(boolgranted,CFErrorReferror){dispatch_semaphore_signal(sema);});
dispatch_semaphore_wait(sema,DISPATCH_TIME_FOREVER);
dispatch_release(sema);
}
//判断授权状态
if(ABAddressBookGetAuthorizationStatus()!=kABAuthorizationStatusAuthorized){
return;
}
//获取通讯录中的所有人
CFArrayRefallPeople=ABAddressBookCopyArrayOfAllPeople(addressBooks);
//通讯录中人数
CFIndexnPeople=ABAddressBookGetPersonCount(addressBooks);
NSMutableArray*persons=[[NSMutableArrayalloc]init];
for(inti=0;i<nPeople;i++){
//获取个人
ABRecordRefperson=CFArrayGetValueAtIndex(allPeople,i);
//获取个人名字
NSString*firstName=(NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);
NSString*lastName=(NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty);
NSMutableString*name=[[NSMutableStringalloc]init];
if(firstName==nil&&lastName==nil){
NSLog(@"名字不存在的情况");
name=nil;
}
if(lastName){
[nameappendString:lastName];
}
if(firstName){
[nameappendString:firstName];
}
ABMultiValueReftmlphone=ABRecordCopyValue(person,kABPersonPhoneProperty);
NSString*telphone=(NSString*)ABMultiValueCopyValueAtIndex(tmlphone,0);
if(telphone!=nil){
telphone=[telphonestringByReplacingOccurrencesOfString:@"-"withString:@""];
NSString*title=[NSStringstringWithFormat:@"%@(%@)",name,telphone];
[personsaddObject:title];
}
}
//对联系人进行分组和排序
UILocalizedIndexedCollation*theCollation=[UILocalizedIndexedCollationcurrentCollation];
NSIntegerhighSection=[[theCollationsectionTitles]count];//中文环境下返回的应该是27,是a-z和#,其他语言则不同
//_indexArray是右侧索引的数组,也是secitonHeader的标题
_indexArray=[[NSMutableArrayalloc]initWithArray:[theCollationsectionTitles]];
NSMutableArray*newSectionsArray=[[NSMutableArrayalloc]initWithCapacity:highSection];
//初始化27个空数组加入newSectionsArray
for(NSIntegerindex=0;index<highSection;index++){
NSMutableArray*array=[[NSMutableArrayalloc]init];
[newSectionsArrayaddObject:array];
[arrayrelease];
}
for(NSString*pinpersons){
//获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11
NSIntegersectionNumber=[theCollationsectionForObject:pcollationStringSelector:@selector(getFirstLetter)];
//把name为“林丹”的p加入newSectionsArray中的第11个数组中去
NSMutableArray*sectionNames=newSectionsArray[sectionNumber];
[sectionNamesaddObject:p];
}
for(inti=0;i<newSectionsArray.count;i++){
NSMutableArray*sectionNames=newSectionsArray[i];
if(sectionNames.count==0){
[newSectionsArrayremoveObjectAtIndex:i];
[_indexArrayremoveObjectAtIndex:i];
i--;
}
}
//_contacts是联系人数组(确切的说是二维数组)
self.contacts=newSectionsArray;
[newSectionsArrayrelease];
[self.tableViewreloadData];
}
顺便把索引和tableViewdataSource的代理方法也贴一下:
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
returnself.contacts.count;
}
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section
{
return[self.contacts[section]count];
}
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*identifier=@"contactCell";
UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:identifier];
if(cell==nil){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];
}
cell.imageView.image=[UIImageimageNamed:@"default_head"];
cell.textLabel.text=[self.contactsobjectAtIndex:indexPath.section][indexPath.row];
returncell;
}
-(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section
{
return[_indexArrayobjectAtIndex:section];
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView
{
return_indexArray;
}
//索引列点击事件
-(NSInteger)tableView:(UITableView*)tableViewsectionForSectionIndexTitle:(NSString*)titleatIndex:(NSInteger)index
{
returnindex;
}
还有两个很重要的方法:
下面这个方法是[theCollationsectionForObject:pcollationStringSelector:@selector(getFirstLetter)];是这里的p对象要实现的方法,我这里的p是NSString,你也可以用其他对象例如Person。
NSString*ret=@"";
if(![selfcanBeConvertedToEncoding:NSASCIIStringEncoding]){//如果是英语
if([[selfletters]length]>2){
ret=[[selfletters]substringToIndex:1];
}
}
else{
ret=[NSStringstringWithFormat:@"%c",[selfcharacterAtIndex:0]];
}
returnret;
}
下面这个方法是NSString得类别方法
-(NSString*)letters{
NSMutableString*letterString=[NSMutableStringstring];
intlen=[selflength];
for(inti=0;i<len;i++)
{
NSString*oneChar=[[selfsubstringFromIndex:i]substringToIndex:1];
if(![oneCharcanBeConvertedToEncoding:NSASCIIStringEncoding]){
NSArray*temA=makePinYin2([oneCharcharacterAtIndex:0]);
if([temAcount]>0){
oneChar=[temAobjectAtIndex:0];
}
}
[letterStringappendString:oneChar];
}
returnletterString;
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!