0

我可以从 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)

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

4

5 回答 5

2

检查这个:

你的问题可以用这个答案这个来解决。

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

希望这可以帮助。

于 2012-05-07T12:00:30.233 回答
2
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];

}
于 2012-05-07T12:10:09.277 回答
1

尝试以下操作:

ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(record, kABPersonPhoneProperty);
NSArray *phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);

for (id number in phoneNumbers) {                    
    // do whatever you want
}
于 2012-05-07T12:05:04.050 回答
1

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

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];

    }

它会工作..

于 2012-05-07T12:09:08.567 回答
1
-(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);

    }
}
于 2013-07-23T08:09:58.430 回答