如何从AddressBook获取联系人并将其存储在iOS 7中的数组中?

时间:2014-04-17 08:55:35

标签: ios abaddressbook

我正在尝试从地址簿中获取联系人然后将其存储在数组中。我无法执行此操作。我检查了堆栈溢出的各种帖子,我正在编写相同的代码但无法执行此操作?< / p>

CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil) {
    NSLog(@"Succesful.");
    allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
    NSLog(@"allContacts.count = %d",[allContacts count]);}    allContact count is always zero

2 个答案:

答案 0 :(得分:1)

@property (nonatomic, assign) ABAddressBookRef addressBook;

//检查用户权限

 -(void)requestAddressBookAccess
{
    ViewController * __weak weakSelf = self;

    ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error)
                                             {
                                                 if (granted)
                                                 {
                                                     dispatch_async(dispatch_get_main_queue(), ^{
                                                         [weakSelf accessGrantedForAddressBook];

                                                     });
                                                 }
                                                 else{
                                                 }
                                             });
}

//获取所有相关内容

 -(void)accessGrantedForAddressBook{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
                                                               kCFAllocatorDefault,
                                                               CFArrayGetCount(people),
                                                               people
                                                               );
    NSMutableArray *allNames = (__bridge NSMutableArray*)peopleMutable;
}

答案 1 :(得分:0)

这可能是因为您没有请求用户访问联系人。 只需将其添加到您的代码中即可。

#import <AddressBookUI/AddressBookUI.h>

  // 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];
      } else {
          // User denied access
          // Display an alert telling user the contact could not be added
      }
    });
  }
  else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // The user has previously given access, add the contact
    [self _addContactToAddressBook];
  }
  else {
    // The user has previously denied access
    // Send an alert telling user to change privacy setting in settings app
  }