问题
I suppose this is largely a question about how iOS handles deprecating APIs.
I have an app that supports iOS 8.3+, and it worked fine through 9.3.x, but it breaks on iOS 10. When my app lets the user pick a contact, the error that I get is:
Terminating app due to uncaught exception 'CNPropertyNotFetchedException', reason: 'A property was not requested when contact was fetched.'
I found another post here which says that ABAddressBook APIs are deprecated and that I should use CNContactPickerViewController (and related ContactsUI classes) instead. I understand the answer that moving away from deprecated classes and to new & improved classes is the recommended solution. But why do I have to?
I thought that deprecated classes and methods are usually still supported in later versions. It makes no sense to me that my app would work fine on iOS 9.3 and then crash on 10.0, especially given that it compiles just fine (the deployment target is "8.3" and the base SDK is "Latest (10.0)".) Furthermore, some code still works (`ABAddressBookCreateWithOptions').
Finally, is there a good way to analyze the code and highlight all such use of deprecated APIs? My build output doesn't show any such warnings.
回答1:
I recently dealt with this myself. The problem has to do with permissions.
Make sure you call ABAddressBookGetAuthorizationStatus()
and if the result is kABAuthorizationStatusNotDetermined
then you must call ABAddressBookRequestAccessWithCompletion
and make use of the completion handler. Only use other address book APIs (including the people picker) after your app has been granted permission.
Attempts to use ABPeoplePickerNavigationController
to allow a user to select a contact's property without first ensuring your app has permission to access the address book will result in the error posted in your question.
来源:https://stackoverflow.com/questions/39812649/why-do-the-deprecated-abaddressbook-apis-crash-ios-10