2010-07-06 157 views
3

我想列出地址簿中所有人的電話號碼(或任何其他字段)。在沒有GUI的情況下獲取iPhone地址簿內容

我已經寫了下面的代碼:

- (void)addressBookFill{ 
    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 
    [addressBook release]; 
} 

- (void)printAddressBook{ 
    for(id person in people){ 
     NSLog(@"%@", [person class]); 
     NSLog(@"\t%@", person); 
    } 
} 

當我調用printAddressBook方法我得到這個我的控制檯上:

2010-07-06 10:34:11.998 app[91420:207] __NSCFType 
2010-07-06 10:34:11.999 app[91420:207] <CPRecord: 0x5d56ce0 ABPerson> 

而且我沒有任何想法,如何解除引用此ABPerson對象,如何從中獲取任何信息。

我想:

firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); 

,但我有一些例外。

任何人都可以告訴我如何從這些對象獲取一些信息嗎?

回答

2

閱讀有關ABPerson類的文檔:
http://developer.apple.com/mac/library/documentation/UserExperience/Reference/AddressBook/Classes/ABPerson_Class/Reference/Reference.html

,也是ABRecord類:

http://developer.apple.com/mac/library/documentation/UserExperience/Reference/AddressBook/Classes/ABRecord_Class/Reference/Reference.html#//apple_ref/occ/cl/ABRecord

[ person valueForProperty: @"propName" ] 

你可以得到可用的屬性有:

[ ABPerson properties ] 

[編輯]

在iPhone上,你可以使用下面的代碼訪問值:

NSString * lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 
+0

恐怕沒有東西ABPerson類iPhone上。只有一些ABPersonRef或ABRecord喜歡這裏: http://developer.apple.com/iphone/library/documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html 和 http://developer.apple.com/ iphone/library/documentation/AddressBook/Reference/ABRecordRef_iPhoneOS/Reference/reference.html#// apple_ref/c/func/ABRecordCopyValue – Alistra 2010-07-06 09:12:13

+0

請參閱編輯:) – Macmade 2010-07-06 09:41:48

1

可能是這樣的代碼將幫助您爲iOS5的,請喜歡

ABAddressBookRef addressBook = ABAddressBookCreate(); 
    NSArray *arrayOfAllPeople = (__bridge_transfer NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook); 
    NSUInteger peopleCounter = 0; 
    for (peopleCounter = 0;peopleCounter < [arrayOfAllPeople count]; peopleCounter++){ 
     ABRecordRef thisPerson = (__bridge ABRecordRef) [arrayOfAllPeople objectAtIndex:peopleCounter]; 
     NSString *name = (__bridge_transfer NSString *) ABRecordCopyCompositeName(thisPerson); 
     NSLog(@"First Name = %@", name); 

     ABMultiValueRef emails = ABRecordCopyValue(thisPerson, kABPersonEmailProperty); 

     for (NSUInteger emailCounter = 0; emailCounter < ABMultiValueGetCount(emails); emailCounter++){ 
      /* And then get the email address itself */ 
      NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, emailCounter); 
      NSLog(@"Email : %@",email); 
     } 
    } 
    CFRelease(addressBook); 
1

你只想得到那些在我的聯繫人誰有我的聯繫人的電子郵件,所以使用此代碼爲iOS5

請喜歡它,如果它工作

首先添加AddressBook.framework#import <AddressBook/AddressBook.h>

- (NSArray*)printAddressBook{ 
     NSMutableArray *mutableData = [NSMutableArray new]; 
     ABAddressBookRef addressBook = ABAddressBookCreate(); 
     NSArray *arrayOfAllPeople = (__bridge_transfer NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook); 
     NSUInteger peopleCounter = 0; 
     for (peopleCounter = 0;peopleCounter < [arrayOfAllPeople count]; peopleCounter++){ 
      ABRecordRef thisPerson = (__bridge ABRecordRef) [arrayOfAllPeople objectAtIndex:peopleCounter]; 
      NSString *name = (__bridge_transfer NSString *) ABRecordCopyCompositeName(thisPerson); 
      NSLog(@"First Name = %@", name); 

      ABMultiValueRef emails = ABRecordCopyValue(thisPerson, kABPersonEmailProperty); 

      for (NSUInteger emailCounter = 0; emailCounter < ABMultiValueGetCount(emails); emailCounter++){ 
       /* And then get the email address itself */ 
       NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, emailCounter); 
       NSLog(@"Email : %@",email); 
       NSMutableDictionary *personDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:name,@"name",email,@"email", nil]; 
       [mutableData addObject:personDict]; 
      } 
     } 
     CFRelease(addressBook); 
     return [NSArray arrayWithArray:mutableData]; 
    } 
相關問題