【问题标题】:Contact numbers iphone sdk联系电话 iphone sdk
【发布时间】:2012-05-07 11:43:05
【问题描述】:

我可以从 iphone sdk 中提取联系人列表的名字和姓氏,但是我无法从中获取电话号码。如果我尝试其他方式,我会收到错误消息,而我使用电话号码获取其他内容的通常方式是代码的详细信息:

- (IBAction)buttonmessage {
    ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book reference object
    NSArray *abContactArray = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); // get address book contact array

    NSInteger totalContacts = [abContactArray count];

    for(NSUInteger loop= 0 ; loop < totalContacts; loop++)
    {
        ABRecordRef record = (__bridge ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record

        if(ABRecordGetRecordType(record) ==  kABPersonType) // this check execute if it is person group
        {
            ABRecordID recordId = ABRecordGetRecordID(record); // get record id from address book record

            recordIdString = [NSString stringWithFormat:@"%d",recordId]; // get record id string from record id

            firstNameString = (__bridge NSString*)ABRecordCopyValue(record,kABPersonFirstNameProperty); // fetch contact first name from address book  
            lastNameString = (__bridge NSString*)ABRecordCopyValue(record,kABPersonLastNameProperty); // fetch contact last name from address book

            NSString *phnumber = (__bridge NSString *)ABRecordCopyValue(record, kABPersonPhoneProperty);

            myArray2 = [NSArray arrayWithObjects:firstNameString,lastNameString,phnumber,nil];

            NSString *m12=[NSString stringWithFormat:@"%@,%@,%@",[myArray2 objectAtIndex:0],[myArray2 objectAtIndex:1],[myArray2 objectAtIndex:2]];
        }

输出:

Abdullah,Rashed,ABMultiValueRef 0x80426a0 with 1 value(s)
    0: _$!<Mobile>!$_ (0x8042da0) - 0550979691 (0x8042dc0)
2012-05-07 14:43:06.670 Firstphase[2914:207] Hussain,Mahmood,ABMultiValueRef 0x80442d0 with 1 value(s)
    0: _$!<Mobile>!$_ (0x8044290) - 055979896 (0x80442b0)


2012-05-07 14:43:06.671 Firstphase[2914:207] Nasir,Jilaani,ABMultiValueRef 0x8046070 with 1 value(s)
    0: _$!<Mobile>!$_ (0x8046000) - 055982391 (0x8046020)


2012-05-07 14:43:06.673 Firstphase[2914:207] Ghulam,Basith,ABMultiValueRef 0x8046850 with 1 value(s)
    0: _$!<Mobile>!$_ (0x8046810) - 055871943 (0x8046830)

但是,如果您仔细查看,我可以在没有任何额外内容的情况下获得名字和姓氏。但我无法以同样的方式获取电话号码。

【问题讨论】:

  • 如果以下答案之一解决了您的问题,请将其标记为答案,以帮助未来的网站访问者找到类似问题的答案。

标签: ios cocoa-touch contacts abaddressbook


【解决方案1】:
ABMutableMultiValueRef multi;
int multiCount = 0;
multi = ABRecordCopyValue(record, kABPersonPhoneProperty);
multiCount = ABMultiValueGetCount(multi);
for (int i = 0; i < multiCount; i++) {

    phoneNumber = (NSString * ) ABMultiValueCopyValueAtIndex(multi, i);
    [someArray addObject: phoneNumber];

}

【讨论】:

    【解决方案2】:

    检查一下:

    answerthis 可以解决您的问题。

    据我了解,ABRecordCopyValue(ref, kABPersonPhoneProperty) 返回一些数组值。而且您正试图获得一个字符串,这就是您可能面临问题的原因。我没有尝试过这个解决方案,但认为它会起作用。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      尝试以下方法:

      ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(record, kABPersonPhoneProperty);
      NSArray *phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
      
      for (id number in phoneNumbers) {                    
          // do whatever you want
      }
      

      【讨论】:

        【解决方案4】:

        以下代码将从联系人列表中检索所有电话号码:-

        ABAddressBookRef addressBook = ABAddressBookCreate( );
            CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
            CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
            for ( int i = 0; i < nPeople; i++ )
            {
                ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
        
                ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(ref, kABPersonPhoneProperty);
                NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
                CFRelease(phoneNumberProperty);
                [phoneNumbers release];
        
            }
        

        它会起作用的..

        【讨论】:

          【解决方案5】:
          -(void)displayPerson
          { 
          
          
          
           CFErrorRef error = NULL;
          
          
            ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
          
              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];
                      NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
                      NSString *lastName =  (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
                      NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
                      NSString *phone=nil;
                      ABMultiValueRef phoneNumbers = ABRecordCopyValue(contactPerson,                                                                                kABPersonPhoneProperty);
                      if (ABMultiValueGetCount(phoneNumbers) > 0) {
                          phone = (__bridge_transfer NSString*)
                          ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
                      } else {
                          phone = @"[None]";
                      }
                      person.fullname = fullName;
                      person.phoneNum=phone;
                      [self.tableData addObject:person];
                      person=nil;
              }
               CFRelease(addressBook);
          
              }
          }
          

          【讨论】:

          • 感谢您的解决方案,但我选择了上面选择的解决方案。无论如何,再次感谢。
          • 这个人的数据类型是什么
          • person 是 NSObject 类。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-05-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-11
          • 1970-01-01
          • 2015-09-05
          相关资源
          最近更新 更多