【问题标题】:Contact Address book crash on iOS 10 betaiOS 10 beta 上的联系人通讯录崩溃
【发布时间】:2016-07-12 10:21:25
【问题描述】:

当点击地址簿中的任何联系人(在我的应用程序内)时,它会在 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

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

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

}

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

【问题讨论】:

  • 我们需要查看堆栈跟踪。
  • 我是怎么得到的?
  • 你有客观的c代码吗?
  • 没有,但很容易转换目标 C
  • 最后的帮助,我如何从 CNContacts 对象中获取名称和编号?当我使用“givenname”或“nameprefix”或namesuffix时,我只得到部分名称

标签: ios contacts addressbook ios10


【解决方案1】:

尝试使用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];

委托示例:

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

    CNPhoneNumber *phoneNumber = contactProperty.value;
    NSString *phoneStr = phoneNumber.stringValue;
}

【讨论】:

  • 次要修复:peoplePicker.displayedPropertyKeys = arrkeys; 应为 peoplePicker.displayedPropertyKeys = arrKeys;
  • 已修复。谢谢@比利(:
【解决方案2】:

地址簿 API 在 iOS 9 中被弃用,取而代之的是更面向对象的 Contacts Framework

不要使用 ABPeoplePickerViewController,而是转到 CNContactPickerViewController

【讨论】:

  • 既然 CNContactPickerViewController 从 iOS 9 可用,我应该如何同时支持 iOS 8 和 iOS 10?
  • @Kenpachi 您必须检查系统版本。如果是 iOS 9 或 10,则需要使用 Contacts 框架。如果是iOS 8,需要使用通讯录。
【解决方案3】:

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

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

Apple Forum thread

【讨论】:

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

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

@[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactEmailAddressesKey]

当他们从 CNContact 对象请求时。

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

【讨论】:

    【解决方案5】:

    iOS 10 不允许访问联系人,直到我们提到我们为什么使用它。打开您的 plist 作为源代码在 dict 下添加以下代码现在再次运行它。

    <key>NSContactsUsageDescription</key>
    <string>$(PRODUCT_NAME) uses Contact</string>
    

    【讨论】:

      【解决方案6】:

      IOS 10 现在需要用户权限才能访问媒体库、照片、 相机和其他类似的硬件。解决方案是添加 他们进入 info.plist 的键,并为用户描述我们的情况 使用他们的数据,iOS 已经需要权限才能访问 麦克风、摄像头和媒体库较早(iOS6、iOS7),但从 iOS10 如果您不提供原因说明,应用程序将崩溃 正在请求许可。

      您可以在 Info.plist 文件中指定所有可可键的列表

      照片:

      Key       :  Privacy - Photo Library Usage Description    
      Value   :  $(PRODUCT_NAME) photo use
      

      麦克风:

      Key        :  Privacy - Microphone Usage Description    
      Value    :  $(PRODUCT_NAME) microphone use
      

      相机:

      Key       :  Privacy - Camera Usage Description   
      Value   :  $(PRODUCT_NAME) camera use
      

      【讨论】:

        【解决方案7】:

        斯威夫特 3

        // This example allows the display and selection of email addresses only.
        if #available(iOS 9.0, *) {
            let picker = CNContactPickerViewController()
            let arrKeys = [CNContactEmailAddressesKey] // array of properties to display
            picker.displayedPropertyKeys = arrKeys
            picker.delegate = self
            present(picker, animated: true, completion: nil)
        }
        

        代理示例

            @available(iOS 9.0, *)
            func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
                let emailAddress = contactProperty.value as? String     
            }
        

        【讨论】:

          【解决方案8】:

          首先在info.plist中添加NSContactsUsageDescription,然后将控制器呈现在AB请求访问块中。

          ABAddressBookRequestAccessWithCompletion(contactPicker, { success, error in
            if success {
              self.presentViewController(self.contactPicker, animated: true, completion: nil)
            }
          })
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多