ABPeoplePickerNavigationController

Discussion in 'iOS Programming' started by XcodeNewb, May 23, 2009.

  1. XcodeNewb macrumors member

    Joined:
    Feb 6, 2009
    #1
    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
     
  2. XcodeNewb thread starter macrumors member

    Joined:
    Feb 6, 2009
  3. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #3
    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;
         
    
    
    
    }
     
  4. XcodeNewb thread starter macrumors member

    Joined:
    Feb 6, 2009
  5. snakeninny macrumors newbie

    Joined:
    Sep 3, 2014
    #5
    people are copying and pasting the same mistake everywhere, so your correct solution is very precious :) thank you! you've made my day:D
     

Share This Page