Cannot select contact on iOS 8

2020-04-07 04:09发布

I have a subclass of ABPeoplePickerNavigationController to handle selecting a contact phone number in my app. Everything works great on iOS 7 and below.

On iOS 8, however, my ABPeoplePickerNavigationControllerDelegate does not get hit when selecting a phone number. Instead, it just calls that phone number.

I noticed that the method I was using to handle contact selection in iOS 7 (peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:) was deprecated in iOS 8. This method was replaced by peoplePickerNavigationController:didSelectPerson:property:identifier:.

I know my delegate is set because I successfully receive the peoplePickerNavigationControllerDidCancel: method callback.

Has anyone else experienced this issue?

Here's a code snippet of my ABPeoplePickerNavigationController subclass:

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {

    [self peoplePickerNavigationController:peoplePicker shouldContinueAfterSelectingPerson:person property:property identifier:identifier];
}

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

    ...do stuff...

    return NO;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    return YES;
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {

    [self dismissViewControllerAnimated:self.shouldAnimateDismiss completion:NULL];
}

4条回答
贪生不怕死
2楼-- · 2020-04-07 04:52

Where are you specifying the peoplePickerDelegate?

In iOS 8, if you specify peoplePickerDelegate in viewDidLoad, you will experience the curious behavior you describe (cancel delegate works, the didSelect... and shouldContinue... do not). If you specify peoplePickerDelegate immediately after init (or during), it works fine.

This would appear to be an iOS 8 "feature". I'll file a bug report.

查看更多
女痞
3楼-- · 2020-04-07 05:04

Nothing would happen when I selected a contact in IOS8.

I found that in addition to

if ([picker respondsToSelector:@selector(setPredicateForSelectionOfPerson:)]) 
    {
         picker.predicateForSelectionOfPerson = [NSPredicate predicateWithFormat:@"emailAddresses.@count = 1"];
    }

I also needed

if ([picker respondsToSelector:@selector(setPredicateForEnablingPerson:)])
    {
        picker.predicateForEnablingPerson = [NSPredicate predicateWithFormat:@"emailAddresses.@count > 0"];
    }

Source https://developer.apple.com/library/prerelease/ios/samplecode/PeoplePicker/Listings/PeoplePicker_AAPL_8or7_EmailPickerViewController_m.html

查看更多
欢心
4楼-- · 2020-04-07 05:10

above two delegate methods are deprecated in ios 8.0, use use methods last two for getting your desire result

above two delegate methods are deprecated in iOS 8.0, use last two methods for getting your desire result

this is apple developer guideline link give you more information about

ABPeoplePickerNavigationControllerDelegate

查看更多
家丑人穷心不美
5楼-- · 2020-04-07 05:16

If you wanna just get person's name you can do this :

-(IBAction)btnGetContact{
    ABPeoplePickerNavigationController *personPicker = [ABPeoplePickerNavigationController new];
    personPicker.peoplePickerDelegate = self;
    [self presentViewController:personPicker animated:YES completion:nil];
}

-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
        NSString *firstName;
    NSString *middleName;
    NSString *lastName;
    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 personPicture
    if (person != nil && ABPersonHasImageData(person))
    {
        retrievedImage = [UIImage imageWithData:(__bridge_transfer NSData*)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];
    }
    else
    {
        retrievedImage = nil;
    }
}

But if you are looking to go to person's detail to get person's numbers, you should use BOOL instead void for peoplePickerNavigationController and pass YES like below :

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
    return YES;
}



 -(void) peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
        ABMutableMultiValueRef phoneno  = ABRecordCopyValue(person, kABPersonPhoneProperty);

CFStringRef phone = ABMultiValueCopyValueAtIndex(phoneno, identifier);

        _mPhone.text = (__bridge NSString *)phone;

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

Also dont forget to import AddressBook.framework and AddressBookUI.framework to your project and ABPeoplePickerNavigationControllerDelegate ,

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

to your header file.

查看更多
登录 后发表回答