无需GUI获取iPhone通讯录内容

3
我希望列出通讯录中所有人的电话号码(或任何其他字段)。
我已经编写了以下代码:
- (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个回答

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

很抱歉,在iPhone上没有ABPerson类。只有一些像这样的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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接