2010-11-24 65 views

回答

18

下面是從我的一個地址簿方法中提取的示例。我沒有通過電話號碼搜索,但這給了你一個想法如何繼續與你需要的東西:

- (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); 
    } 
+2

AlBeebe找到聯繫人, 不幸的是它需要更多的時間在設備上(iPhone 4,4.2)。我花了15秒鐘的地址簿3000) – Rabih 2011-02-25 12:39:15

-1

使用此。這是我的代碼。

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; 
} 
5
-(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秒,2.5K接觸,以填補 那麼你可以使用數

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

其超快需要像0.000x秒