代码之家  ›  专栏  ›  技术社区  ›  Alistra

在没有GUI的情况下获取iPhone地址簿内容

  •  3
  • Alistra  · 技术社区  · 16 年前

    我想列出所有电话号码(或任何其他领域)的人在通讯录。

    我编写了以下代码:

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

    我试过:

    firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    

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

    3 回复  |  直到 16 年前
        1
  •  2
  •   Macmade    16 年前

    阅读关于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 );
    
        2
  •  1
  •   gauravds    14 年前

    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);
    
        3
  •  1
  •   gauravds    14 年前

    你只想得到那些在我的联系人上有电子邮件的联系人,所以在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];
        }
    
    推荐文章