在iOS 10测试版上联系地址簿崩溃

Ban*_*ore 18 contacts addressbook ios ios10

当点击地址簿中的任何联系人(在我的应用程序内)时,它在iOS 10测试版上崩溃并在iOS 9版本上运行良好;

这是崩溃日志

*** Terminating app due to uncaught exception 'CNPropertyNotFetchedException', reason: 'A property was not requested when contact was fetched.'
*** First throw call stack:
(0x1cf11a07 0x1c62af63 0x1cf1194d 0x246f0f4f 0x246c6a71 0x1ce355eb 0x1ce2e19b 0x246c69cf 0x246c6883 0x25e4a375 0x2538f283 0x254204ef 0x25420bb1 0xe9da97 0xe9da83 0xea2321 0x1cecf18f 0x1cecd477 0x1ce1e6bd 0x1ce1e549 0x1e54ebfd 0x21f961e3 0x21f90947 0x966c9 0x1ca9e507)
libc++abi.dylib: terminating with uncaught exception of type NSException
Run Code Online (Sandbox Code Playgroud)

以下是在我的应用程序中打开地址簿的代码:

-(void)showContactPicker {
__weak RecieverSelectorViewController *weakSelf = self;
    ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    picker.modalPresentationStyle = UIModalPresentationFullScreen;
    picker.modalTransitionStyle = UIModalPresentationPopover;
    [self presentViewController:picker
                       animated:YES
                     completion:^{
                         [weakSelf hideLoadingAnimation];

                         // animation to show view controller has completed.
                     }];
}



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

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

-(void)setSelectedPerson:(ABRecordRef)person {


    NSString *contactName = CFBridgingRelease(ABRecordCopyCompositeName(person));

    ABMultiValueRef phoneRecord = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFStringRef phoneNumber = ABMultiValueCopyValueAtIndex(phoneRecord, 0);

    self.isSenderReciever = NO;

    NSString *phone = [PorterUtils
                       extraLast10DigitsFromDigitString:[PorterUtils
                                                         extractNumberFromText:(__bridge_transfer NSString *)phoneNumber]];




    //Handling Social Media Contacts - Crash

    if(contactName.length>0 && phone.length>0){

      [self setRecieverName:contactName
                   number:phone];
       CFRelease(phoneRecord);
    }

}
Run Code Online (Sandbox Code Playgroud)

它仅在iOS 10公共测试版上崩溃.

erd*_*ser 11

在iOS 9中不推荐使用Address Book API,而是使用更面向对象的Contacts Framework.

而不是使用ABPeoplePickerViewController,移动到CNContactPickerViewController.


Ela*_*lad 11

尝试使用CNContactPickerViewController(iOS9及以上版本):

添加ContactsUI.framework,导入框架,声明委托CNContactPickerDelegate.

实现它(在Objective-C中):

CNContactPickerViewController *peoplePicker = [[CNContactPickerViewController alloc] init];
    peoplePicker.delegate = self;
    NSArray *arrKeys = @[CNContactPhoneNumbersKey]; //display only phone numbers
    peoplePicker.displayedPropertyKeys = arrKeys;
    [self presentViewController:peoplePicker animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

代表示例:

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{ 

    CNPhoneNumber *phoneNumber = contactProperty.value;
    NSString *phoneStr = phoneNumber.stringValue;
}
Run Code Online (Sandbox Code Playgroud)


小智 6

检查您是否提供了有效的密钥,例如

@[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactEmailAddressesKey]
Run Code Online (Sandbox Code Playgroud)

当他们向 CNContact 对象请求时。

例如:如果您需要调用 contact.emailAddresses,则必须从 (CNContactEmailAddressesKey) 数组提供。


Ami*_*aju 5

将"隐私 - 联系人使用说明"添加到您的info.plist.

Apple论坛也提出了同样的问题.GWesley的原始答案如下.

Apple论坛帖子

  • 您描述的问题与OP获得的错误(CNPropertyNotFetchedException)不同.虽然您的解决方案适合您的问题,但在这种情况下它是不正确的:) (4认同)