【问题标题】:all contact numbers not coming from the address book所有不是来自通讯录的联系电话
【发布时间】:2015-01-29 06:04:14
【问题描述】:

我从通讯录中遇到了一个问题,我从我的联系人中保存了两个电话号码,一个作为家,另一个作为办公室,我在应用程序联系人列表中只得到一个号码。如果我想得到这两个号码,我该怎么办想做。

【问题讨论】:

  • 请贴一些代码。
  • @LyndseyScott :感谢 Lyndsey 尝试解决我的问题,但是,我解决了这个问题,只是循环中有一点点错误。

标签: ios iphone ios7 ios8 addressbook


【解决方案1】:
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];

                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2012-12-10
    相关资源
    最近更新 更多