Why do the deprecated ABAddressBook APIs crash iOS 10?

一世执手 提交于 2020-01-05 05:34:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!