49

由于我已将 iPhone 上的 XCode(6.0、6A313)和我的 iOS(8.0、12A365)更新为 gm 种子,因此 ABPeoplePickerNavigationController 代码无法像以前那样工作。

  • iOS 7.1.2:如果有人想导入联系人,地址簿会打开,你会看到完整的联系人列表,选择一个后,它会打开联系人的详细视图,然后你可以通过点击手机添加联系人您要导入的号码。

  • iOS 8.0:一切都相似,但如果你点击联系人的号码,它会拨打电话号码而不是导入它..

代码:

#pragma mark - AddressBook Delegate Methods

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


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

    // Get the first and the last name. Actually, copy their values using the person object and the appropriate
    // properties into two string variables equivalently.
    // Watch out the ABRecordCopyValue method below. Also, notice that we cast to NSString *.
    NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

    // Compose the full name.
    NSString *fullName = @"";
    // Before adding the first and the last name in the fullName string make sure that these values are filled in.
    if (firstName != nil) {
        fullName = [fullName stringByAppendingString:firstName];
    }
    if (lastName != nil) {
        fullName = [fullName stringByAppendingString:@" "];
        fullName = [fullName stringByAppendingString:lastName];
    }


    // Get the multivalue number property.
    CFTypeRef multivalue = ABRecordCopyValue(person, property);

    // Get the index of the selected number. Remember that the number multi-value property is being returned as an array.
    CFIndex index = ABMultiValueGetIndexForIdentifier(multivalue, identifier);

    // Copy the number value into a string.
    NSString *number = (__bridge NSString *)ABMultiValueCopyValueAtIndex(multivalue, index);

    nameTextField.text = fullName;
    numberTextField.text = number;

    // Dismiss the contacts view controller.
    [_addressBookController dismissViewControllerAnimated:YES completion:nil];

    return NO;
}


// Implement this delegate method to make the Cancel button of the Address Book working.
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
    [_addressBookController dismissViewControllerAnimated:YES completion:nil];
}

在苹果的 iOS 开发者库中找不到任何答案。有其他人的解决方案吗?

4

3 回答 3

79

iOS 8 需要为此实现一个新的委托方法:

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

保留旧的委托方法以支持 iOS 7 或更早版本。我在我的应用程序中所做的是从 iOS 8 委托方法调用 iOS 7 委托方法:

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
    [self peoplePickerNavigationController:peoplePicker shouldContinueAfterSelectingPerson:person property:property identifier:identifier];
}

如果此委托方法未在 iOS 8 中实现,则点击该值会导致该操作。实施时,将使用选定的值调用委托。

于 2014-09-12T16:08:59.573 回答
13

另请参阅 iOS8 新增的委托方法:

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person;
{
    [self selectedPerson:person];
}

这就是我想要的。

于 2014-09-20T04:09:17.487 回答
1

这对我适用于 iOS 8 和 iOS 7 及更低版本。

注意我使用这个 didSelectPerson:(ABRecordRef)person 代替。

//Needed for iOS 8
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    NSLog(@"Went here 1 ...");

    [self peoplePickerNavigationController:peoplePicker shouldContinueAfterSelectingPerson:person];
}


//needed for iOS 7 and lower
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{

    NSLog(@"Went here 2 ...");

    //add your logic here

}
于 2014-12-27T18:05:07.213 回答