Monday, May 16, 2011

Displaying ABPeoplePickerNavigationController and Getting Address Details

-(void)showPeoplePickerController
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    // Display only a person's phone, email, and birthdate
    NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
                                [NSNumber numberWithInt:kABPersonEmailProperty],
                                [NSNumber numberWithInt:kABPersonBirthdayProperty],[NSNumber numberWithInt:kABPersonAddressProperty],nil];
   
    NSLog(@"kABPersonAddressProperty %@",[NSNumber numberWithInt:kABPersonAddressProperty]);
    picker.displayedProperties = displayedItems;
    // Show the picker
    [self presentModalViewController:picker animated:YES];
    [picker release];   
}
#pragma mark ABPeoplePickerNavigationControllerDelegate methods
// Displays the information of a selected person
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{   
       ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonAddressProperty);
    NSMutableArray *theArray = [(id)ABMultiValueCopyArrayOfAllValues(multi) autorelease];
    //const NSUInteger theIndex =[NSUInteger integerValue:0];
    NSMutableDictionary *theDict = [theArray objectAtIndex:0];
    const NSUInteger theCount = [theDict count];
    NSString *keys[theCount];
    NSString *values[theCount];
    [theDict getObjects:values andKeys:keys];
    NSString *address;
    address = [NSString stringWithFormat:@"%@, %@, %@, %@ %@",
               [theDict objectForKey:(NSString *)kABPersonAddressStreetKey],
               [theDict objectForKey:(NSString *)kABPersonAddressCityKey],
               [theDict objectForKey:(NSString *)kABPersonAddressStateKey],
               [theDict objectForKey:(NSString *)kABPersonAddressZIPKey],
               [theDict objectForKey:(NSString *)kABPersonAddressCountryKey]];
    NSLog(@"address %@",address);
    NSString *name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:name message:address delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
    [alert show];
    return NO;
}

No comments:

Post a Comment