Apple在其QuickContacts项目中提供了以下示例代码,以了解如何在通讯录中搜索特定用户。

-(void)showPersonViewController
{
 // Fetch the address book
 ABAddressBookRef addressBook = ABAddressBookCreate();

 // Search for the person named "Appleseed" in the address book
 CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, CFSTR("Appleseed"));

 // Display "Appleseed" information if found in the address book
 if ((people != nil) && (CFArrayGetCount(people) > 0))
 {
  ABRecordRef person = CFArrayGetValueAtIndex(people, 0);
  ABPersonViewController *picker = [[[ABPersonViewController alloc] init] autorelease];
  picker.personViewDelegate = self;
  picker.displayedPerson = person;
  // Allow users to edit the person’s information
  picker.allowsEditing = YES;

  [self.navigationController pushViewController:picker animated:YES];
 }
 else
 {
  // Show an alert if "Appleseed" is not in Contacts
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
              message:@"Could not find Appleseed in the Contacts application"
                delegate:nil
             cancelButtonTitle:@"Cancel"
             otherButtonTitles:nil];
  [alert show];
  [alert release];
 }
 CFRelease(addressBook);
 CFRelease(people);
}


我遇到的问题是:

// Search for the person named "Appleseed" in the address book
CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, CFSTR("Appleseed"));


这将在通讯录中搜索名为“ Appleseed”的人,但是我想根据存储在变量中的用户搜索通讯录。例如,我正在尝试:

Customer *customer = [customerArray objectAtIndex:indexPath.row];
cell.textLabel.text = customer.name;

CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, CFSTR(customer.name));


“ customer.name”没有解析为所存储的值。我使用NSLog输出customer.name的值,它保存了预期值。

如何将这个变量解析为字符串,以便它可以正确搜索通讯簿?

谢谢!

最佳答案

customer.name是NSString吗?

CFSTR仅接受文字C字符串。
要传递NSString,请将其强制转换为CFStringRef:

CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook,
                                        (CFStringRef)customer.name);

关于iphone - 如何根据变量而不是字符串在iPhone通讯簿中搜索特定的用户名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2930134/

10-13 03:51