无需GUI即可获取iPhone地址簿内容

时间:2010-07-06 08:42:05

标签: iphone objective-c addressbook core-foundation

我想列出地址簿中所有人的电话号码(或任何其他字段)。

我写了以下代码:

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

但我有一些例外。

有人能告诉我如何从这些物品中获取一些信息吗?

3 个答案:

答案 0 :(得分: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 );

答案 1 :(得分: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);

答案 2 :(得分: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];
    }