SoFunction
Updated on 2025-04-12

Get local address book contacts and Chinese characters in IOS

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

/*** Load local contacts*/ 
- (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 permission    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++) { 
    //Get individual    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); 
    //Get your 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(@"There is no name"); 
      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]; 
    } 
  } 
   
  //Group and sort contacts  UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation]; 
  NSInteger highSection = [[theCollation sectionTitles] count]; //The returned should be 27 in Chinese environment, a-z and #, and other languages ​​are different   
  //_indexArray is an array of indexes on the right, and is also the title of the secitonHeader  _indexArray = [[NSMutableArray alloc] initWithArray:[theCollation sectionTitles]]; 
   
  NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:highSection]; 
  //Initialize 27 empty arrays and add 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 where the name attribute value is located, such as "Lin Dan", the first letter is L, ranking 11th in A~Z (the first position is 0), and the sectionNumber is 11    NSInteger sectionNumber = [theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)]; 
    // Add the p with name "Lin Dan" to the 11th array in newSectionsArray    NSMutableArray *sectionNames = newSectionsArray[sectionNumber]; 
    [sectionNames addObject:p]; 
  } 
   
  for (int i = 0; i < ; i++) { 
    NSMutableArray *sectionNames = newSectionsArray[i]; 
    if ( == 0) { 
      [newSectionsArray removeObjectAtIndex:i]; 
      [_indexArray removeObjectAtIndex:i]; 
      i--; 
    } 
  } 
   
  //_contacts is a contact array (to be precise, a two-dimensional array)   = newSectionsArray; 
  [newSectionsArray release]; 
   
  [ reloadData]; 
} 

By the way, I will also post the proxy method of the index and tableView dataSource:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
  return ; 
} 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
  return [[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]; 
  } 
   
   = [UIImage imageNamed:@"default_head"]; 
   = [ objectAtIndex:][]; 
  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 two more important methods:

The following method is[theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)]; This is the method to implement the p object here. My p here is NSString, and 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 the 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 it can help you. Thank you for your support for this site!