【问题标题】:Search ABAddressbook iOS SDK搜索 ABAddressbook iOS SDK
【发布时间】:2011-03-01 10:41:17
【问题描述】:

我想在 iPhone 通讯簿中搜索特定电话号码,然后检索联系人姓名。我目前正在遍历所有联系人并提取多值属性并与值进行比较。这需要太多时间。我已经阅读了 Apple 通讯录指南,他们说:

"完成其他类型的搜索,使用函数 ABAddressBookCopyArrayOfAllPeople 然后使用过滤结果 NSArray 方法filteredArrayUsingPredicate:."

谁能给我一个具体的例子吗?

谢谢。

【问题讨论】:

标签: iphone objective-c ios sdk addressbook


【解决方案1】:

如果你想通过电话号码在联系人中进行搜索,那么我会告诉你一个建议。

1.获取所有联系人

NSArray *thePeoples = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);

2.从联系人数组(thePeoples)创建另一个数组(记录),

记录:[ 记录1, 记录2, ....记录N]

记录:{name:"myContactName", 电话号码:“1234567890”}

3.使用谓词搜索 mutableArray(records)。

NSPredicate * myPredicate = [NSPredicate predicateWithFormat:@"record.phoneNumber contains %@",string];

NSArray * filteredArray = [records filteredArrayUsingPredicate:myPredicate];

这只是您解决方案的一个简单示例,请记住 phoneNumber 是一个多值字段。因此,我们将在模型类变量中包含一个数组作为电话号码。

【讨论】:

  • 从联系人数组创建另一个数组是什么意思?
  • 它是如何工作的? [<__nscftype> valueForUndefinedKey:]:此类不符合键记录的键值编码。
【解决方案2】:

以下方法将返回一个数组,其中包含具有给定电话号码的所有联系人。此方法在我运行 iOS7 的 iPhone 5 上搜索 250 个联系人需要 0.02 秒。

#import <AddressBook/AddressBook.h>


-(NSArray *)contactsContainingPhoneNumber:(NSString *)phoneNumber {
    /*

     Returns an array of contacts that contain the phone number

     */

    // Remove non numeric characters from the phone number
    phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];

    // Create a new address book object with data from the Address Book database
    CFErrorRef error = nil;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    if (!addressBook) {
        return [NSArray array];
    } else if (error) {
        CFRelease(addressBook);
        return [NSArray array];
    }

    // Requests access to address book data from the user
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {});

    // Build a predicate that searches for contacts that contain the phone number
    NSPredicate *predicate = [NSPredicate predicateWithBlock: ^(id record, NSDictionary *bindings) {
        ABMultiValueRef phoneNumbers = ABRecordCopyValue( (__bridge ABRecordRef)record, kABPersonPhoneProperty);
        BOOL result = NO;
        for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
            NSString *contactPhoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
            contactPhoneNumber = [[contactPhoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];
            if ([contactPhoneNumber rangeOfString:phoneNumber].location != NSNotFound) {
                result = YES;
                break;
            }
        }
        CFRelease(phoneNumbers);
        return result;
    }];

    // Search the users contacts for contacts that contain the phone number
    NSArray *allPeople = (NSArray *)CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
    NSArray *filteredContacts = [allPeople filteredArrayUsingPredicate:predicate];
    CFRelease(addressBook);

    return filteredContacts;
}

【讨论】:

    【解决方案3】:

    使用这个。这是我的代码。 制作数组进行搜索。

            NSLog(@"=====Make People Array with Numbers. Start.");
            peopleWithNumber = [[NSMutableDictionary alloc] init];
            for (int i=0; i < [people count]; i++) {
                NSInteger phoneCount = [self phoneCountAtIndex:i];
                if (phoneCount != 0) {
                    NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
                    for (int j=0 ; j < phoneCount ; j++) {
                        [phoneNumbers addObject:[self phoneNumberAtIndex:i phoneIndex:j]];
                    }
                    [peopleWithNumber addEntriesFromDictionary:
                     [NSDictionary dictionaryWithObjectsAndKeys:
                      [NSArray arrayWithArray:phoneNumbers],    [self fullNameAtIndex:i], nil]];
                }
            }
            NSLog(@"=====Make People Array with Numbers. End.\n");
    

    搜索方法。 it(peopleWithNumber) 会比使用 array(people) 更快

    "NSArray *people = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);"

    - (NSArray *)searchNamesByNumber:(NSString *)number {
    
        NSString *predicateString = [NSString stringWithFormat:@"%@[SELF] contains '%@'",@"%@",number];
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:predicateString,peopleWithNumber,number];
        NSArray *names = [[peopleWithNumber allKeys] filteredArrayUsingPredicate:searchPredicate];
    
        return names;
    }
    

    【讨论】:

      【解决方案4】:

      您不能将 predicateWithFormat 与 ABRecordRef 不透明类型的数组一起使用。但是你可以使用 predicateWithBlock:

      [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
          ABRecordRef person=(__bridge ABRecordRef)evaluatedObject;
          CFTypeRef theProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
          NSArray *phones = (__bridge_transfer NSArray *) ABMultiValueCopyArrayOfAllValues(theProperty);
          CFRelease(theProperty);
          BOOL result=NO;
          for (NSString *value in phones) {
              if ([value rangeOfString:@"3"].location!=NSNotFound) {
                  result=YES;
                  break;
              }
          }
          return result;
      }];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-11
        • 2016-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多