-3

嗨,我正在开发一个应用程序,因为我有一个要求,当我单击按钮时,我有一个按钮操作方法,我应该检索整个通讯簿联系人并以字符串的形式显示我该怎么做,任何人都可以用代码告诉我。几天后我无法做到这一点。

不想使用ABPeoplePickerNavigationController和访问它,我只需要单击按钮访问所有联系人并以字符串的形式显示。

4

2 回答 2

1

试试这个,它适用于iOS 6 以及 iOS 5.0 或更早版本

首先在Link Binary With Libraries中添加以下框架

  • AddressBookUI.framework
  • 地址簿框架

他们进口

#import <AddressBook/ABAddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

然后使用下面的代码

ABAddressBookRef addressBook = ABAddressBookCreate();

__block BOOL accessGranted = NO;

if (ABAddressBookRequestAccessWithCompletion != NULL) { // We are on iOS 6
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(semaphore);
    });

    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    dispatch_release(semaphore);
}

else { // We are on iOS 5 or Older
    accessGranted = YES;
    [self getContactsWithAddressBook:addressBook];
}

if (accessGranted) {
    [self getContactsWithAddressBook:addressBook];
}

// Get the contacts.
- (void)getContactsWithAddressBook:(ABAddressBookRef )addressBook {
    NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    NSUInteger index = 0;
    NSMutableArray *firstNames = [[NSMutableArray alloc] init];

    for(index = 0; index <= ([arrayOfPeople count] - 1); index++){
        ABRecordRef currentPerson = (__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];
        NSString *currentFirstName = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty);

        // If first name is empty then don't add to the array
        if (![currentFirstName length] == 0) {
            [firstNames addObject: currentFirstName];
        }

    }
    //OPTIONAL: Use the following line to sort the first names alphabetically
    NSArray *sortedFirstNames = [firstNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSLog(@"Total Count = %d \n Sorted By Name = %@", [sortedFirstNames count], sortedFirstNames);
}

我对此进行了测试,并且可以正常工作。

于 2013-05-31T05:07:22.430 回答
0

试试这个。

    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
    for(id person in people){
         NSString *name = [NSString stringWithFormat:@"%@ %@",(NSString *)ABRecordCopyValue( person, kABPersonFirstNameProperty ),(NSString *)ABRecordCopyValue( person, kABPersonLastNameProperty )];
    //fetch multiple phone nos.
        ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) {
            NSString* phone = (NSString*)ABMultiValueCopyValueAtIndex(multi, j);
            [numbersArray addObject:phone];
            [phone release];
        }

          /fetch multiple email ids.
        ABMultiValueRef multiemail = ABRecordCopyValue(person, kABPersonEmailProperty);
        for (CFIndex j=0; j < ABMultiValueGetCount(multiemail); j++) {
             NSString* email = (NSString*)ABMultiValueCopyValueAtIndex(multiemail, j);
             NSLog(@"Email=%@",email);
        }
    }

你必须在使用前分配你的数组。在 viewDidLoad 方法中为 alloc 数组写这个

           numbersArray=[[NSMutableArray alloc] init];
于 2013-05-31T05:23:12.707 回答