ios - 在iOS8中获取contactID时出错-崩溃

标签 ios crash ios8 contacts

我一直在使用iOS应用程序,当我需要从手机中获取联系信息时,这是我的代码:

ABRecordID contactID = ABRecordGetRecordID(contact);
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(nil, nil);
ABRecordRef origContactRef = ABAddressBookGetPersonWithRecordID(addressBook, contactID);

CFRetain(origContactRef);
...
...
...
CFRelease(origContactRef);

问题在iOS8中,在此行中:CFRetain(origContactRef);
因为对象为空。

我该如何解决?

问候

最佳答案

如果contactID为-1,则以下其余语句将失败。

在这种情况下,-1表示找不到记录。

您需要随时检查错误,您不能仅仅假设每个方法调用都会成功。在这种情况下,第一个检查错误的地方是ABRecordGetRecordID语句。

Apple documentation:

ABRecordGetRecordID

Return Value The unique ID of record when the record exists in the Address Book database.

kABRecordInvalidID when the record hasn’t been saved to the database.




#define kABRecordInvalidID -1

关于ios - 在iOS8中获取contactID时出错-崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26574833/

相关文章:

ios - dateFromString在ios 6中工作,但不在ios 5中工作

ios - 缩放 UIImage 以正确适应 UIImageView

crash - Android 应用程序在 Jetpack Compose 项目中卡住而不是崩溃

iphone - 如何安全地停止 AVAudioPlayer

swift - 在 EKEventStore 中创建 EKCalendar - Swift

storyboard - 呈现警报 Controller 时 UIPopOverController 缩小

ios - 为什么类型转换有效而选择器不起作用

ios - 为 iOS 8 中的新语言自定义键盘系统范围

android - Activity 死亡重新启动进程

ios - IOS 8 中的预处理器宏和 bool 值未正确评估