所有联系号码都不是来自地址簿

时间:2015-01-29 06:04:15

标签: ios iphone ios7 ios8 addressbook

我从地址簿中遇到一个问题,我从我的联系人中保存了两个电话号码,其中一个作为办公室,另外一个作为办公室,我只得到一个号码,应用程序联系人列表。如果我想得到两个号码是什么我想做。

1 个答案:

答案 0 :(得分:0)

With this code you will fetch all contact names from address book.I hope it will help you.


   -(void)addressbookfetch
    {   
      CFErrorRef error = NULL;
      ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
      CFArrayRef people=ABAddressBookCopyArrayOfAllPeople(addressBook);
       CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
                                                            kCFAllocatorDefault,
                                                             CFArrayGetCount(people),
                                                           people
                                                           );

       CFArraySortValues(
                  peopleMutable,
                  CFRangeMake(0, CFArrayGetCount(peopleMutable)),
                  (CFComparatorFunction) ABPersonComparePeopleByName,
                  (void*) ABPersonGetSortOrdering()
                  );
      if(addressBook != nil)
     {
        NSLog(@"Succesful.");
        NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

        NSUInteger i = 0;
        for(i=0;i<[allContacts count];i++)
        {
            Person *person = [[Person alloc]init];

            ABRecordRef contactPerson = (__bridge  ABRecordRef)allContacts[i];


           ABMultiValueRef phoneNumber = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
           NSUInteger k=0;
           for(k=0;k<ABMultiValueGetCount(phoneNumber);k++)
           {
               NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
               NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

               NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName ?: @"",lastName ?: @""];

               fullName = [fullName stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
            [addressData addObject:fullName];
              NameStr = [[NSMutableString alloc] initWithString:fullName];
            NameStrnew=[NSString stringWithString:fullName];
               NSLog(@"Name string %@",NameStrnew);
              [phoneBookNames addObject:NameStrnew];



               NSString *phonenumber= (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneNumber, k);
               if(k==0)

              {
                  person.homePhone = phonenumber;
                  PhoneStr = [[NSMutableString alloc] initWithString:phonenumber];
                   PhoneStrnew =[NSString stringWithString:phonenumber];
                    NSLog(@"PhoneStrnew%@",PhoneStrnew);

                // [addressData addObject:phonenumber];
                   [phoneBookNumbers addObject:PhoneStrnew];

                }