gpt4 book ai didi

ios - ABRecordCopyValue 因 SIGSEGV 而崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:01 25 4
gpt4 key购买 nike

我正在尝试获取这样的联系人列表:

CFErrorRef *error = nil;
ABAddressBookRef addressBook = nil;

__block BOOL accessGranted = NO;

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {

addressBook = ABAddressBookCreateWithOptions(NULL, error);
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted) {
return nil;
}

if (!accessGranted) {

if (addressBook) CFRelease(addressBook);
return nil;
}

CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

if (nPeople <= 0) {
CFRelease(addressBook);
return nil;
}

ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);

if (!allPeople) return nil;

NSMutableArray *contactsArray = [NSMutableArray arrayWithCapacity:nPeople];

for (CFIndex i = 0; i < nPeople; ++i) {

ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

if (!person) continue;

ContactData *contact = [ContactData new];
contact.firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

它在我的 iPhone 5s 和模拟器上运行,但构建在带有 SIGSEGV 的测试仪 iPod 设备上崩溃:

   contact.firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

这是堆栈:

3 AppSupport 0x34129a04 CPRecordCopyProperty4 AppSupport 0x34129a04 CPRecordCopyProperty5 地址簿 0x2fd6ad22 ABRecordCopyValueUnfiltered6 地址簿 0x2fd6abc6 ABRecordCopyValue

最佳答案

我有同样的错误,问题是:

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);

CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

给出了不同数量的联系人(所以在你的情况下 nPeople 可能比 allPeople 大,这会导致崩溃)。 “来源”似乎并没有提供地址簿中的所有联系人。将其更改为 nil 为我解决了这个问题。另外,为了确保我会这样做:nPeople = CFArrayGetCount(allPeople);

Jokinryou Tsui 在这篇文章中很好地解释了解决方案:ABAddressBookCopyArrayOfAllPeople and ABAddressBookGetPersonCount return different sizes

(这是我的第一篇文章,所以我不确定我是否违反了任何规则或遵循了正确的程序。希望答案对您有所帮助!)

关于ios - ABRecordCopyValue 因 SIGSEGV 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24285835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com