Cannot select contact on iOS 8

£可爱£侵袭症+ 提交于 2019-12-01 17:55:17

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!