Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

XcodeNewb

macrumors member
Original poster
Feb 6, 2009
79
0
I am trying to get a phone number from the contacts picker. My problem is that the identifier provided by the shouldContinueAfterSelectingPerson function is not consistent and is causing the app to crash.

Here is the code for that function:
Code:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
	
        NSLog(@"Here ident[%d]", identifier);
	
	ABMultiValueRef container = ABRecordCopyValue(person, property);
	CFStringRef contactData = ABMultiValueCopyValueAtIndex(container, identifier);
	CFRelease(container);
	NSString *contactString = [NSString stringWithString:(NSString *)contactData];
	CFRelease(contactData);
	
	NSLog(@"PhoneNumber[%@]", contactString);
	
	[self dismissModalViewControllerAnimated:YES];	
	return NO;
}

The problem is that when I choose the first phone number listed for the contact person sometimes the identifier is given as a "0" and sometimes it is given as a "1" and sometimes a "2".

So when I try and run the command:

Code:
	CFStringRef contactData = ABMultiValueCopyValueAtIndex(container, identifier);

The app will crash if the identifier is given as a "2" and there is only one phone number. It is trying to access an object at index 2 when it does not exist.

Why would I be getting back a bad identifier number?

Please keep in mind this is my first dealing with the contact list so I may be completely missing something here.

Thanks
 

MacDonaldsd

macrumors 65816
Sep 8, 2005
1,005
0
London , UK
You need to use the ABMultiValueGetIndexForIdentifier() to get the index. The identifier isn't the index of that property.

Code:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{	

	ABMultiValueRef multiValue = ABRecordCopyValue(person, property);
	
	NSString *number = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, ABMultiValueGetIndexForIdentifier(multiValue, identifier));

        //Copy the number etc before cleaning everything up
	
        [number release];
	CFRelease(multiValue);

        return NO;
     



}
 

snakeninny

macrumors newbie
Sep 3, 2014
1
0
You need to use the ABMultiValueGetIndexForIdentifier() to get the index. The identifier isn't the index of that property.

Code:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{	

	ABMultiValueRef multiValue = ABRecordCopyValue(person, property);
	
	NSString *number = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, ABMultiValueGetIndexForIdentifier(multiValue, identifier));

        //Copy the number etc before cleaning everything up
	
        [number release];
	CFRelease(multiValue);

        return NO;
     



}

people are copying and pasting the same mistake everywhere, so your correct solution is very precious :) thank you! you've made my day:D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.