user2007117
user2007117

Reputation:

How to pick a contact from address book ios8

I have been working on a iOS application a year a go with ios7 now I upgraded it to ios8 , and saw that there is a new way of picking contacts.

what I do now is :

-(void)showABNewPersonViewController{
    //Calling the addresbook
    ABPeoplePickerNavigationController *picker =
    [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{

    //Getting contact email and phone number then assign it to there tb's.
    ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
    CFStringRef emailID = ABMultiValueCopyValueAtIndex(emails, 0);
    _tbContactPersonEmail.text = [NSString stringWithFormat:@"%@", emailID];
    CFRelease(emailID);
    CFRelease(emails);

    ABMultiValueRef phone = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFStringRef phoneID = ABMultiValueCopyValueAtIndex(phone, 0);
    _tbContactPersonPhone.text = [NSString stringWithFormat:@"%@", phoneID];
    CFRelease(phoneID);
    CFRelease(phone);

    [self fieldValueChanged:nil];

    [self dismissViewControllerAnimated:YES completion:nil];
    return NO;
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    return NO;
}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
    [self dismissViewControllerAnimated:YES completion:nil];
}

And with that code i can open the address book and select a contact , but now it is not returning his/her name and number its opening a kind of detail view of the selected contact .

my peoplePickerNavigationController is not getting called

Thanks for help and fast answer ¨

Upvotes: 5

Views: 5909

Answers (3)

ricardopereira
ricardopereira

Reputation: 11683

Here's an example in Swift:

Call the Address book picker:

@IBAction func didTouchAddContactButton(sender: AnyObject) {
    let contactPicker = ABPeoplePickerNavigationController()
    contactPicker.peoplePickerDelegate = self
    // Just to pick an email
    let properties: [AnyObject] = [Int(kABPersonEmailProperty)]
    contactPicker.displayedProperties = properties
    // Open picker
    presentViewController(contactPicker, animated: true, completion: nil)
}

Get the email from Person:

extension ViewController: ABPeoplePickerNavigationControllerDelegate {
    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {

        // Property
        let list: ABMultiValueRef = ABRecordCopyValue(person, property).takeRetainedValue() //kABPersonEmailProperty
        // Value index
        let index = Int(identifier) as CFIndex
        // Retrieve value
        let email: String = ABMultiValueCopyValueAtIndex(list, index).takeRetainedValue() as! String

        // Result
        print(email)

        peoplePicker.dismissViewControllerAnimated(true, completion: nil)
    }   
}

Upvotes: 2

Razvan
Razvan

Reputation: 4122

To be able to select a contact in iOS 8 you need to use this:

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {

....// do whatever you need here

}

Of course you need to conform to ABPeoplePickerNavigationControllerDelegate first.

Example:

-(void)openPeoplePicker
{
        ABPeoplePickerNavigationController *personPicker = [ABPeoplePickerNavigationController new];

        personPicker.peoplePickerDelegate = self;
        [self presentViewController:personPicker animated:YES completion:nil];
}

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {

    NSString *firstName;
    NSString *middleName;
    NSString *lastName;
    NSDate *retrievedDate;
    UIImage *retrievedImage;

    // get the first name
    firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

    //get the middle name
    middleName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);

    // get the last name
    lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

    // get the birthday
    retrievedDate = (__bridge_transfer NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);

    // get personPicture
    if (person != nil && ABPersonHasImageData(person))
    {
        retrievedImage = [UIImage imageWithData:(__bridge_transfer NSData*)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];
    }
    else
    {
        retrievedImage = nil;
    }

    //set the name
    if (firstName != NULL && middleName != NULL && lastName != NULL)
    {
        retrievedName = [[NSString alloc] initWithFormat:@"%@ %@ %@",firstName,middleName,lastName];
    }

    if (firstName != NULL && middleName != NULL & lastName == NULL)
    {
        retrievedName = [[NSString alloc] initWithFormat:@"%@ %@",firstName, middleName];
    }

    if (firstName != NULL && middleName == NULL && lastName != NULL)
    {
        retrievedName = [[NSString alloc] initWithFormat:@"%@ %@",firstName,lastName];
    }

    if (firstName != NULL && middleName == NULL && lastName == NULL)
    {
        retrievedName = [[NSString alloc] initWithFormat:@"%@",firstName];
    }

    if (firstName == NULL && middleName != NULL && lastName != NULL)
    {
        retrievedName = [[NSString alloc] initWithFormat:@"%@ %@",middleName, lastName];
    }

    if (firstName == NULL && middleName != NULL && lastName == NULL)
    {
        retrievedName = [[NSString alloc] initWithFormat:@"%@",middleName];
    }

    if (firstName == NULL && middleName == NULL && lastName != NULL)
    {
        retrievedName = [[NSString alloc] initWithFormat:@"%@", lastName];
    }

            [self dismissViewControllerAnimated:NO completion:^(){}];
}

Upvotes: 12

Soumya Ranjan
Soumya Ranjan

Reputation: 4843

I update my code. This is my updated code. Its working 100% perfect.

 - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
   ABMultiValueRef phone = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);
   CFStringRef phoneID = ABMultiValueCopyValueAtIndex(phone, 0);
   phoneNumberTextField.text = [NSString stringWithFormat:@"%@", phoneID];
   CFRelease(phoneID);
   CFRelease(phone);

   [self dismissViewControllerAnimated:YES completion:nil];

}

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
   return NO;
}

-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
   [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addContactFromPhoneContacts:(id)sender
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

Upvotes: 0

Related Questions