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

发布于 2024-09-08 15:30:40 字数 799 浏览 0 评论 0原文

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

我编写了以下代码:

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

但我遇到了一些例外。

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

I would like to list all phone numbers (or any other field) of people in the addressbook.

I've written the following code:

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

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

When I invoke the printAddressBook method I get this on my console:

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

And I don't have any idea, how to dereference this ABPerson object, how to get any info from it.

I tried:

firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);

but I got some exceptions.

Can anybody tell me how to get some info from these objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尸血腥色 2024-09-15 15:30:40

阅读有关 ABPerson 类的文档:
http://开发人员。 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 );

Read the documentation about the ABPerson class:
http://developer.apple.com/mac/library/documentation/UserExperience/Reference/AddressBook/Classes/ABPerson_Class/Reference/Reference.html

and also the ABRecord class:

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

[ person valueForProperty: @"propName" ]

You can get the available properties with:

[ ABPerson properties ]

[ EDIT ]

On iPhone, you can use the following code to access a value:

NSString * lastName = (NSString *)ABRecordCopyValue( person, kABPersonLastNameProperty );
倾`听者〃 2024-09-15 15:30:40

也许这个代码对你的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);

May be this code will help you for iOS5, please like it

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);
笑着哭最痛 2024-09-15 15:30:40

您只想获取那些在我的联系人上有电子邮件的联系人,因此请在 iOS5 上使用此代码,

如果它有效,请喜欢它。

首先添加 AddressBook.framework#import

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

You want get only those contact who has email on my contact so use this code for iOS5

Please like it if it work

First add AddressBook.framework and #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];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文