【问题标题】:How to search the iphone address book for a specific phone number?如何在 iphone 通讯录中搜索特定的电话号码?
【发布时间】:2011-05-15 09:40:44
【问题描述】:

我正在开发一个使用 bonjour 连接到另一部 iphone 的应用程序。它的一个功能是当我连接到其他设备时,它会自动检查我是否有其他人的电话号码。所以我的问题是如何检查我的通讯录中其他设备提供的电话号码?

【问题讨论】:

    标签: iphone objective-c ios addressbook


    【解决方案1】:

    这是从我的地址簿方法之一中提取的示例。我没有通过电话号码进行搜索,但这让您了解如何继续满足您的需求:

    - (void) scanAddressBookSample
        {
        NSUInteger i;
        NSUInteger k;
    
        ABAddressBookRef addressBook = ABAddressBookCreate();
        NSArray *people = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
    
        if ( people==nil )
            {
            NSLog(@"NO ADDRESS BOOK ENTRIES TO SCAN");
            CFRelease(addressBook);
            return;
            }
    
        for ( i=0; i<[people count]; i++ )
            {
            ABRecordRef person = (ABRecordRef)[people objectAtIndex:i];
    
            //
            // Phone Numbers
            //
            ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
            CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );
    
            for ( k=0; k<phoneNumberCount; k++ )
                {
                CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, k );
                CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, k );
                CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );    // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"
    
                // Find the ones you want here
                //
                NSLog(@"-----PHONE ENTRY -> %@ : %@", phoneNumberLocalizedLabel, phoneNumberValue );
    
                CFRelease(phoneNumberLocalizedLabel);
                CFRelease(phoneNumberLabel);
                CFRelease(phoneNumberValue);
                }
            }
    
        [people release];
        CFRelease(addressBook);
        }
    

    【讨论】:

    • AlBeebe,不幸的是,在设备(iPhone 4、4.2)上花费的时间要多得多。我花了 15 秒的时间来获取 3000 个地址簿)
    • iphone 3GS ios4.2.2 => 2000 个联系人需要更多 40 秒。这不好。
    【解决方案2】:

    使用这个。这是我的代码。

    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");
    

    搜索方法。它会比使用数组更快

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

    【讨论】:

      【解决方案3】:
      -(void)createQuickAccessContacts{
      
          NSMutableDictionary contactDictionary= [[NSMutableDictionary alloc]init];
      
      
          CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook);
          CFIndex n = ABAddressBookGetPersonCount(addressBook);
      
          NSDate *date=[NSDate date];
      
          for( int i = 0 ; i < n ; i++ )
          {
              ABRecordRef ref = CFArrayGetValueAtIndex(all, i);
              ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
      
              for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
              {
      
                  CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
                  NSString *phoneNumber = (__bridge NSString *)phoneNumberRef;
                  [contactDictionary setObject:(__bridge id)(ref) forKey:phoneNumber];
      
      
              }
          }
      
          NSLog(@" Time taken %f for %i contacts",[[NSDate date] timeIntervalSinceDate:date],[contactDictionary count]);
      
      }
      

      这需要 0.5 秒才能填充 2500 个联系人 然后您可以使用号码找到联系人

       ABRecordRef ref= [contactDictionary objectForKey:@"89xxxxxxx"];
      

      它的超快速度大约需要 0.000 倍秒

      【讨论】:

        猜你喜欢
        • 2010-12-30
        • 2012-12-10
        • 1970-01-01
        • 1970-01-01
        • 2011-07-19
        • 1970-01-01
        • 2012-10-27
        • 2013-11-14
        • 1970-01-01
        相关资源
        最近更新 更多