0

I'm able to pull the first name and the last name of the contact list from iphone sdk, however I'm unable to fetch the phone number from it. I'm getting the error if I try other way, and the usual way I'm getting other stuff with phone number here is the details with the code:

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

Output:

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)

However if you take a close look i'm able to get the firstname and last name without any extra stuff. But I'm not able to get the phone numbers in the same way.

obaid
  • 864
  • 11
  • 25
  • If one of the below answers solved your problem, mark it as the answer to help future site visitors find answers to similar problems. – pasawaya Jun 27 '12 at 04:25

5 Answers5

2

check this:

Your problem may solve with this answer or this.

As of I understand ABRecordCopyValue(ref, kABPersonPhoneProperty) returns some array value. And you are trying to get a String thats why you may face the issue. I didn't tried this solution but think it will work.

Hope this helps.

Community
  • 1
  • 1
Kapil Choubisa
  • 4,970
  • 8
  • 59
  • 99
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];

}
Tushar Gupta - curioustushar
  • 54,013
  • 22
  • 95
  • 103
Saad
  • 8,501
  • 2
  • 36
  • 49
1

Try the following:

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

for (id number in phoneNumbers) {                    
    // do whatever you want
}
tilo
  • 13,266
  • 6
  • 62
  • 81
1

The following code will retrieve all the phone numbers from the contact list:-

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

    }

It will work..

Anil Kothari
  • 7,433
  • 4
  • 17
  • 24
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);

    }
}
jbchitaliya
  • 211
  • 2
  • 13