简体   繁体   English

无法在iOS 8上选择联系人

[英]Cannot select contact on iOS 8

I have a subclass of ABPeoplePickerNavigationController to handle selecting a contact phone number in my app. 我有一个ABPeoplePickerNavigationController的子类来处理我的应用程序中的联系电话号码。 Everything works great on iOS 7 and below. iOS 7及更低版本的一切都很棒。

On iOS 8, however, my ABPeoplePickerNavigationControllerDelegate does not get hit when selecting a phone number. 但是,在iOS 8上,选择电话号码时,我的ABPeoplePickerNavigationControllerDelegate不会受到影响。 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: . 我注意到我用来处理iOS 7中的联系人选择的方法( peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:在iOS 8中已被弃用。此方法被peoplePickerNavigationController:didSelectPerson:property:identifier:取代peoplePickerNavigationController:didSelectPerson:property:identifier: .

I know my delegate is set because I successfully receive the peoplePickerNavigationControllerDidCancel: method callback. 我知道我的委托已设置,因为我成功收到了peoplePickerNavigationControllerDidCancel:方法回调。

Has anyone else experienced this issue? 还有其他人遇到过这个问题吗?

Here's a code snippet of my ABPeoplePickerNavigationController subclass: 这是我的ABPeoplePickerNavigationController子类的代码片段:

- (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];
}

Where are you specifying the peoplePickerDelegate ? 你在哪里指定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). 在iOS 8中,如果在viewDidLoad指定了peoplePickerDelegate ,您将体验到您描述的好奇行为(取消委托工作, didSelect...shouldContinue...不要)。 If you specify peoplePickerDelegate immediately after init (or during), it works fine. 如果你在init (或期间)之后立即指定了peoplePickerDelegate ,它可以正常工作。

This would appear to be an iOS 8 "feature". 这似乎是iOS 8“功能”。 I'll file a bug report. 我将提交错误报告。

在ios 8.0中不推荐使用上面两个委托方法,使用最后两个方法来获取你想要的结果

above two delegate methods are deprecated in iOS 8.0, use last two methods for getting your desire result 以上两种委托方法在iOS 8.0中已弃用,使用最后两种方法来获得您的期望结果

this is apple developer guideline link give you more information about 这是苹果开发者指南链接,为您提供更多信息

ABPeoplePickerNavigationControllerDelegate ABPeoplePickerNavigationControllerDelegate

Nothing would happen when I selected a contact in IOS8. 当我在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 来源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 ,而不是voidpeoplePickerNavigationController并通过YES象下面这样:

-(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 , 也不要忘记将AddressBook.frameworkAddressBookUI.framework导入到您的项目和ABPeoplePickerNavigationControllerDelegate

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

to your header file. 到你的头文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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