IOS地址簿列表,只返回一个组,而不是全部

时间:2014-05-01 04:25:43

标签: ios addressbook abaddressbook

我正在编写试图解析IOS地址簿联系人列表中所有联系人的代码。这看起来非常简单,我已将联系人和各自的电子邮件作为列表返回。

我注意到的一件事是我的Iphone联系人列表中有多个群组(我看到这个浏览手机的原生联系人列表):

  • 所有Facebook
  • 所有Gmail
  • 全部在我的iPhone上

我的手机当前忽略了Facebook,并在我的列表中显示其他两个。

当我查看代码联系人的输出时,对于我的应用,我意识到它默认只显示“所有Gmail”的联系人。

我是否需要做些什么才能显示“我的iphone上的所有”联系人?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    // Request authorization to Address Book
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {
                // First time access has been granted, add the contact
                [self addContactToAddressBook:addressBookRef];
            } else {
                // User denied access
                // Display an alert telling user the contact could not be added
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Contact list could not be loaded, since you denied access" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
        [self addContactToAddressBook:addressBookRef];
    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Please grant access to your contact list in Iphone Settings" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

-(void)addContactToAddressBook:(ABAddressBookRef)addressBook {
    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
    //CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    CFIndex nPeople = CFArrayGetCount(allPeople);

    self.allContacts = [[NSMutableArray alloc] init];

   // int contactIndex = 0;
    for (int i = 0; i < nPeople; i++) {
        // Get the next address book record.
        ABRecordRef record = CFArrayGetValueAtIndex(allPeople, i);
        CFStringRef firstName, lastName, companyName;

        firstName = ABRecordCopyValue(record, kABPersonFirstNameProperty);
        lastName  = ABRecordCopyValue(record, kABPersonLastNameProperty);
        companyName  = ABRecordCopyValue(record, kABPersonOrganizationProperty);

        NSString *fullName = nil;
        if(firstName == nil && lastName == nil){
            fullName = [NSString stringWithFormat:@"%@", companyName];
        } else {
            if(firstName == nil){
                fullName = [NSString stringWithFormat:@"%@", lastName];
            } if(lastName == nil){
                fullName = [NSString stringWithFormat:@"%@", firstName];
            } else {
                fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
            }
        }

        // Get array of email addresses from address book record.
        ABMultiValueRef emailMultiValue = ABRecordCopyValue(record, kABPersonEmailProperty);
        NSArray *emailArray = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue);

        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:2];
        dict[@"fullname"] = fullName;
        dict[@"emailArr"] = (emailArray != nil ? emailArray : @[]);

       // if(emailArray != nil){
            [self.allContacts addObject:dict];
        //}
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

1 个答案:

答案 0 :(得分:1)

从我的工作应用程序中的代码,要获取通讯簿中的所有联系人,请使用以下代码:

ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, nil);
NSMutableArray *contactRefs = [NSMutableArray array];
NSArray *sources = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllSources(book));
for (id source in sources){
    NSArray *refs = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(book, (__bridge ABRecordRef)(source), ABPersonGetSortOrdering()));
    [contactRefs addObjectsFromArray:refs];
}

基本上,这样做会遍历所有组,并将所有组中的联系人添加到contactRefs