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

时间:2013-09-01 08:33:23

标签: ios arrays abaddressbook

我正在尝试在地址簿中获取联系人的第一个名称,然后将其存储在数组中。但是我无法将联系人存储在数组中。这就是我的尝试。

 -(void)fetchAddressBook
 {
   ABAddressBookRef UsersAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);

  //contains details for all the contacts
  CFArrayRef ContactInfoArray = ABAddressBookCopyArrayOfAllPeople(UsersAddressBook);

  //get the total number of count of the users contact
  CFIndex numberofPeople = CFArrayGetCount(ContactInfoArray);

  //iterate through each record and add the value in the array
    for (int i =0; i<numberofPeople; i++) {
    ABRecordRef ref = CFArrayGetValueAtIndex(ContactInfoArray, i);
    ABMultiValueRef names = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty));

     NSLog(@"name from address book = %@",names);  // works fine.

     NSString *contactName = (__bridge NSString *)(names);
    [self.reterivedNamesMutableArray addObject:contactName];
     NSLog(@"array content = %@", [self.reterivedNamesMutableArray lastObject]);//shows null

}
}

1 个答案:

答案 0 :(得分:1)

完成....数组实际上需要初始化。