2011-07-29 28 views
0

有沒有辦法在用戶的iPhone中獲取對地址簿的引用,過濾出所有不以字母「A」開頭的聯繫人,然後顯示該已過濾的地址簿?這聽起來可能與一個UITableView,但有一個特殊的意見,所有的地址簿功能?只顯示以'A'開頭的地址簿視圖?

+0

沒有,沒有用戶界面,您可以訪問時,這些混沌編程方式訪問的DAT a在幾個答案中是exampled – bshirley

回答

2

爲了讓所有的人,他們的名字的數組A開始,你會使用類似:

ABAddressBook *ab = [ABAddressBook sharedAddressBook]; 
ABSearchElement *startsWithA =[ABPerson searchElementForProperty:kABLastNameProperty 
           label:nil key:nil 
           value:@"A" 
           comparison:kABPrefixMatchCaseInsensitive]; 
NSArray *peopleFound = 
    [ab recordsMatchingSearchElement:startsWithA]; 

一旦你得到一個數組,你可以在任何你需要的自定義視圖中使用它。

+0

這僅是OS X。 iOS沒有ABAddressBook類 – zxcat

0

這段代碼假定filteredPeople是要與誰所有名稱以A.地址簿中的聯繫人來填充表視圖的數據源

這個答案應該幫助你們了:Blank field showing up on iPhone AddressBook, how to debug?

也是蘋果對「直接互動:以編程方式訪問數據庫」的全面,詳細的http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/DirectInteraction.html%23//apple_ref/doc/uid/TP40007744-CH6-SW1

 ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
                   kCFAllocatorDefault, 
                   CFArrayGetCount(people), 
                   people 
                   ); 
    NSMutableArray *allNames = (NSMutableArray*)peopleMutable; 



    filteredPeople = [[NSMutableArray alloc] init ]; 

    for (id person in allNames) { 
     NSMutableString *firstName = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease]; 

     if ([firstName length] > 0){ 
      NSString* firstChar = [firstName substringToIndex:1]; 

      if ([firstChar isEqualToString:@"A"] || [firstChar isEqualToString:@"a"]){ 

       [filteredPeople addObject:person]; 
      } 
     } 
    } 

    [self.theTableView reloadData]; 



    CFRelease(addressBook); 
    CFRelease(people); 
    CFRelease(peopleMutable);