简体   繁体   中英

Cannot select person from Contact List in iOS8

I realize this question has been asked prior but nothing mentioned in those threads worked. Many seemed to have the issue because the delegate was set in viewDidLoad but as you can see from the below, that is not where I am setting mine. The "Cancel" operation DOES work so the delegate works but not for actually selecting.

Protocol declared:

@interface MyTrackDetailsTVC () <UITextFieldDelegate,UIImagePickerControllerDelegate,ABPeoplePickerNavigationControllerDelegate>

Instance variable defined:

@property (strong, nonatomic) ABPeoplePickerNavigationController *picker;

Process initiated from a button tap:

- (IBAction)importContactTapped:(UIButton *)sender
{
    self.picker = [[ABPeoplePickerNavigationController alloc] init];

    self.picker.peoplePickerDelegate = self;

    [self presentViewController:self.picker animated:YES completion:nil];
}

Cancel method works just fine:

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

{
    [[peoplePicker presentingViewController]dismissViewControllerAnimated:YES completion:nil];
}

Selection method never called (verified with break points).

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    NSString *fName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    NSData *imgData = (__bridge_transfer NSData *)ABPersonCopyImageData(person);

    self.nameField.text = [NSString stringWithFormat:@"(%@ %@",fName,lName];
    self.phoneField.text = lName;

    self.imageView.image = [UIImage imageWithData:imgData];

    [[peoplePicker presentingViewController]dismissViewControllerAnimated:YES completion:nil];
}

The peoplePickerNavigationController:didSelectPerson:property:identifier: delegate method is called when a specific property on a contact is selected. If you want to know when a contact is selected, use the peoplePickerNavigationController:didSelectPerson: delegate method.

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    NSString *fName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    NSData *imgData = (__bridge_transfer NSData *)ABPersonCopyImageData(person);

    self.nameField.text = [NSString stringWithFormat:@"(%@ %@",fName,lName];
    self.phoneField.text = lName;

    self.imageView.image = [UIImage imageWithData:imgData];

    [[peoplePicker presentingViewController]dismissViewControllerAnimated:YES completion:nil];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM