Obtaining the Contacts of Local Address Book and Sorting the First Letter of Chinese Characters in IOS

  • 2021-08-28 21:21:57
  • OfStack

Get the contact information in the mobile phone address book in iOS:


/***  Load local contacts */ 
- (void)loadLocalContacts 
{ 
  // New 1 Address book classes  
  ABAddressBookRef addressBooks = nil; 
   
  if (DeviceVersion < 6.0) { 
    addressBooks = ABAddressBookCreate(); 
  } else { 
    addressBooks = ABAddressBookCreateWithOptions(NULL, NULL); 
    // Getting 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 the 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++) { 
    // Acquisition of individuals  
    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 situation where 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]; 
    } 
  } 
   
  // Grouping and sorting contacts  
  UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation]; 
  NSInteger highSection = [[theCollation sectionTitles] count]; // In the Chinese environment, it should be 27 Yes a - z And #, other languages are different  
   
  //_indexArray  Is the array indexed to the right, which is also the secitonHeader Title of  
  _indexArray = [[NSMutableArray alloc] initWithArray:[theCollation sectionTitles]]; 
   
  NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:highSection]; 
  // Initialization 27 Add an empty array newSectionsArray 
  for (NSInteger index = 0; index < highSection; index++) { 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 
    [newSectionsArray addObject:array]; 
    [array release]; 
  } 
   
  for (NSString *p in persons) { 
    // Get name Property, such as the location where the value of the " Lindane " The initials are L , in A~Z Middle rank 11 (Part 1 Bit is 0 ), sectionNumber Just for 11 
    NSInteger sectionNumber = [theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)]; 
    // Put name For "Lindane" p Join newSectionsArray The first of the 11 To the array of  
    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  It's a contact group (to be exact 2 Dimensional array)  
  self.contacts = newSectionsArray; 
  [newSectionsArray release]; 
   
  [self.tableView reloadData]; 
} 

By the way, the index and the proxy method of tableView dataSource are also posted under 1:


- (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; 
} 
 
// Index column click event  
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
  return index; 
} 

There are also two important methods:

The following method is [theCollation sectionForObject: p collationStringSelector: @ selector (getFirstLetter)]; This is the method that the p object will implement. My p is NSString. You can also use other objects such as Person.


 NSString *ret = @""; 
  if (![self canBeConvertedToEncoding: NSASCIIStringEncoding]) {// If it's 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 the class method of 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; 
} 

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


Related articles: