Contact Address book crash on iOS 10 beta

≯℡__Kan透↙ 提交于 2019-11-27 22:03:26

The Address Book API was deprecated in iOS 9 in favor of the more object-oriented Contacts Framework.

Instead of using the ABPeoplePickerViewController, move to CNContactPickerViewController.

Try to use CNContactPickerViewController (iOS9 and above):

Add ContactsUI.framework, import the framework, declare the delegate CNContactPickerDelegate.

Implement it (in 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];

delegate example:

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

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

Add "Privacy - Contacts Usage Description" to your info.plist .

The same question was raised in Apple Forums. The original answer by GWesley is given below.

Apple Forum thread

iOS 10 is not allowing to access the Contact until we mention Why we are using it.open your plist as Source code add the below code under dict Now run it again.

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

check whether you have provided valid keys like

@[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactEmailAddressesKey]

when they are requesting from CNContact object.

ex: if you need to call contact.emailAddresses it must be provided from (CNContactEmailAddressesKey) array.

IOS 10 Now Requires User Permission to Access Media Library, Photos, Camera and other Hardware like these. The solution for this is to add their keys into info.plist with description for user that how we are using their data , iOS already required permissions to access microphone, camera, and media library earlier (iOS6, iOS7), but since iOS10 the apps will crash if you don't provide the description why you are asking for the permission.

There is a list of all Cocoa Keys that you can specify in your Info.plist file

Photo :

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

Microphone :

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

Camera :

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

Swift 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)
}

Delegate example

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

First add NSContactsUsageDescription in info.plist and then present the controller in AB request access block like .

ABAddressBookRequestAccessWithCompletion(contactPicker, { success, error in
  if success {
    self.presentViewController(self.contactPicker, animated: true, completion: nil)
  }
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!