无法在iOS 8上选择联系人

时间:2014-09-24 19:09:47

标签: ios ios8 addressbookui

我有ABPeoplePickerNavigationController的子类来处理在我的应用中选择联系电话号码。 iOS 7及以下版本的所有功能都很棒。

但是,在iOS 8上,选择电话号码时我的ABPeoplePickerNavigationControllerDelegate不会受到影响。相反,它只是拨打该电话号码。

我注意到iOS 8中用于处理联系人选择的方法(peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:)在iOS 8中已弃用。此方法已替换为peoplePickerNavigationController:didSelectPerson:property:identifier:

我知道我的委托已设置,因为我已成功收到peoplePickerNavigationControllerDidCancel:方法回调。

还有其他人遇到过这个问题吗?

以下是我的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];
}

4 个答案:

答案 0 :(得分:6)

您在哪里指定peoplePickerDelegate

在iOS 8中,如果您在peoplePickerDelegate中指定viewDidLoad,则会遇到您描述的奇怪行为(取消委托有效,didSelect...shouldContinue...不行) 。如果您在peoplePickerDelegate(或期间)之后立即指定init,则可以正常使用。

这似乎是iOS 8“功能”。我将提交错误报告。

答案 1 :(得分:2)

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

以上两种委托方法在iOS 8.0中已弃用,使用最后两种方法获取您的期望结果

这是Apple开发者指南链接,为您提供有关

的更多信息

ABPeoplePickerNavigationControllerDelegate

答案 2 :(得分:0)

当我在IOS8中选择联系人时,什么都不会发生。

我发现除

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

我还需要

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

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

答案 3 :(得分:0)

如果你想得到一个人的名字,你可以这样做:

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

但是,如果您希望查看此人的详细信息以获取此人的号码,则应使用BOOL代替void peoplePickerNavigationController并传递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:^(){}];
    }

另外不要忘记将AddressBook.frameworkAddressBookUI.framework导入您的项目并ABPeoplePickerNavigationControllerDelegate

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

到你的头文件。