Get local Address book contacts in IOS and alphabetical sort _ios

Source: Internet
Author: User

iOS to get contact information from the phone address book:

/*** Load Local Contact */-(void) loadlocalcontacts {//Create a new Address Book class Abaddressbookref addressbooks = nil; 
  if (Deviceversion < 6.0) {addressbooks = Abaddressbookcreate (); 
    else {addressbooks = abaddressbookcreatewithoptions (null, NULL); 
    Get Address Book permissions 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); 
  //Judge Authorization status if (Abaddressbookgetauthorizationstatus ()!=kabauthorizationstatusauthorized) {return; 
  //Get everyone in the Address book Cfarrayref allpeople = Abaddressbookcopyarrayofallpeople (addressbooks); 
  Number of people in the address book Cfindex npeople = Abaddressbookgetpersoncount (addressbooks); 
  Nsmutablearray *persons = [[Nsmutablearray alloc] init]; for (int i = 0; i < npeople i++) {//get personal abrecordref person = CfarraygetvalueAtindex (allpeople, i); 
    Get personal name nsstring *firstname = (NSString *) abrecordcopyvalue (person, kabpersonfirstnameproperty); 
    NSString *lastname = (NSString *) abrecordcopyvalue (person, kabpersonlastnameproperty); 
    nsmutablestring *name = [[Nsmutablestring alloc] init]; 
      if (FirstName = = Nil && LastName = = nil) {NSLog (@ "The name does not exist"); 
    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]; To group and sort contacts Uilocalizedindexedcollation *thecollation= [Uilocalizedindexedcollation currentcollation]; Nsinteger highsection = [[Thecollation sectiontitles] count]; Chinese environment should be returned 27, is a-Z and #, other languages are different//_indexarray is the right index of the array, but also the secitonheader title _indexarray = [[Nsmutablearray alloc] I 
   
  Nitwitharray:[thecollation Sectiontitles]]; 
  Nsmutablearray *newsectionsarray = [[Nsmutablearray alloc] initwithcapacity:highsection];  Initializes 27 empty arrays to join Newsectionsarray for (Nsinteger index = 0; index < highsection; index++) {Nsmutablearray *array = 
    [[Nsmutablearray alloc] init]; 
    [Newsectionsarray Addobject:array]; 
  [Array release]; For (NSString *p in persons) {//Get the location of the value of the Name property, such as "Lindane", the first letter is L, 11th in the A~z (first is 0), Sectionnumber is one nsinte 
    Ger Sectionnumber = [thecollation sectionforobject:p collationstringselector: @selector (getfirstletter)]; 
    Add p with Name "Lindane" to the 11th array in Newsectionsarray to 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 is a group of contact numbers (specifically two-dimensional array) self.contacts = Newsectionsarray; 
   
  [Newsectionsarray release]; 
[Self.tableview Reloaddata]; 
 }

By the way, also paste the index and TableView DataSource proxy method:

-(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 N 
  Sstring *identifier = @ "Contactcell"; 
  UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:identifier]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifi 
  ER]; 
  } 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; }//Index column Click Event-(Nsinteger) TableView: (UITableView *) TableView sectionforsectionindextitle: (NSString *) title Atindex: (NS 
Integer) index {return index; 
 }

There are two important ways to do this:

The following method is [thecollation sectionforobject:p collationstringselector: @selector (Getfirstletter)]; Is the method here for the P object to implement, my p here is nsstring, you can also use other objects such as person.

 NSString *ret = @ ""; 
  if (![ Self canbeconvertedtoencoding:nsasciistringencoding]) {//If it is English if 
    ([[Self letters] length]>2) { 
      ret = [ Self letters] substringtoindex:1]; 
    } 
  else { 
    ret = [nsstring stringwithformat:@ '%c ', [self characteratindex:0]]; 
  } 
  return ret; 
} 

The following method is a NSString class method.

-(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; 
} 

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.