• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOS通讯录导入崩溃异常类型: EXC_BAD_ACCESS (SIGSEGV)

[复制链接]
菜鸟教程小白 发表于 2022-12-13 08:32:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

首先简要介绍一下背景,我聘请了一名开发人员来构建我的应用程序,但我知道通过剖析现有代码可以更好地学习,因此我的目标是通过这种方式了解更多有关 iOS 编程的知识,而不是花钱请开发人员来修复。

现在,当我将通讯录中的联系人导入应用程序时,应用程序崩溃了。我注意到当我从 iOS Facebook 联系人而不是其他联系人组导入联系人时,应用程序崩溃。这可能是什么原因造成的?

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000000
Triggered by Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:

研究告诉我,该应用正在尝试调用它无法访问但不确定从这里去哪里的东西。

这是我认为导致问题的代码区域。 (这个 Action )。

-(void)peoplePickerNavigationControllerDidCancelABPeoplePickerNavigationController *)peoplePicker{
[_addressBookController dismissViewControllerAnimated:YES completion:nil];
}

-(BOOL)peoplePickerNavigationControllerABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPersonABRecordRef)person{
_arrContactsData =[[NSMutableArray alloc] init];
NSMutableDictionary *contactInfoDict = [[NSMutableDictionary alloc]
                                        initWithObjects[@"", @"", @"", @"", @"", @"", @"", @"", @""]
                                        forKeys[@"firstName", @"lastName", @"mobileNumber", @"homeNumber", @"homeEmail", @"workEmail", @"address", @"zipCode", @"city"]];

// Use a general Core Foundation object.
CFTypeRef generalCFObject = ABRecordCopyValue(person, kABPersonFirstNameProperty);

// Get the first name.
if (generalCFObject) {
    [contactInfoDict setObject__bridge NSString *)generalCFObject forKey"firstName"];
    CFRelease(generalCFObject);
}

// Get the last name.
generalCFObject = ABRecordCopyValue(person, kABPersonLastNameProperty);
if (generalCFObject) {
    [contactInfoDict setObject__bridge NSString *)generalCFObject forKey"lastName"];
    CFRelease(generalCFObject);
}

// Get the phone numbers as a multi-value property.
ABMultiValueRef phonesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int i=0; i<ABMultiValueGetCount(phonesRef); i++) {
    CFStringRef currentPhoneLabel = ABMultiValueCopyLabelAtIndex(phonesRef, i);
    CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, i);

    if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
        [contactInfoDict setObject__bridge NSString *)currentPhoneValue forKey"mobileNumber"];
    }

    if (CFStringCompare(currentPhoneLabel, kABHomeLabel, 0) == kCFCompareEqualTo) {
        [contactInfoDict setObject__bridge NSString *)currentPhoneValue forKey"homeNumber"];
    }

    CFRelease(currentPhoneLabel);
    CFRelease(currentPhoneValue);
}
CFRelease(phonesRef);


// Get the e-mail addresses as a multi-value property.
ABMultiValueRef emailsRef = ABRecordCopyValue(person, kABPersonEmailProperty);
for (int i=0; i<ABMultiValueGetCount(emailsRef); i++) {
    CFStringRef currentEmailLabel = ABMultiValueCopyLabelAtIndex(emailsRef, i);
    CFStringRef currentEmailValue = ABMultiValueCopyValueAtIndex(emailsRef, i);

    if (CFStringCompare(currentEmailLabel, kABHomeLabel, 0) == kCFCompareEqualTo) {
        [contactInfoDict setObject__bridge NSString *)currentEmailValue forKey"homeEmail"];
    }

    if (CFStringCompare(currentEmailLabel, kABWorkLabel, 0) == kCFCompareEqualTo) {
        [contactInfoDict setObject__bridge NSString *)currentEmailValue forKey"workEmail"];
    }

    CFRelease(currentEmailLabel);
    CFRelease(currentEmailValue);
}
CFRelease(emailsRef);


// Get the first street address among all addresses of the selected contact.
ABMultiValueRef addressRef = ABRecordCopyValue(person, kABPersonAddressProperty);
if (ABMultiValueGetCount(addressRef) > 0) {
    NSDictionary *addressDict = (__bridge NSDictionary *)ABMultiValueCopyValueAtIndex(addressRef, 0);

    [contactInfoDict setObject:[addressDict objectForKeyNSString *)kABPersonAddressStreetKey] forKey"address"];
    [contactInfoDict setObject:[addressDict objectForKey:(NSString *)kABPersonAddressZIPKey] forKey"zipCode"];
    [contactInfoDict setObject:[addressDict objectForKey:(NSString *)kABPersonAddressCityKey] forKey:@"city"];
}
CFRelease(addressRef);


// If the contact has an image then get it too.
if (ABPersonHasImageData(person)) {
    NSData *contactImageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);

    [contactInfoDict setObject:contactImageData forKey:@"image"];
}

// Initialize the array if it's not yet initialized.
if (_arrContactsData == nil) {
    _arrContactsData = [[NSMutableArray alloc] init];
}
// Add the dictionary to the array.
[_arrContactsData addObject:contactInfoDict];

// Reload the table view data.


// Dismiss the address book view controller.
[_addressBookController dismissViewControllerAnimated:YES completion:nil];
NSDictionary *info = [_arrContactsData objectAtIndex:0];
self.txfFirstName.text = [info objectForKey:@"firstName"];
self.txfLastName.text = [info objectForKey:@"lastName"];
self.txfMobile.text =[info objectForKey:@"mobileNumber"];
self.txfEmail.text =[info objectForKey:@"homeEmail"];
NSLog(@"Info %@",info);

return NO;
}



Best Answer-推荐答案


看起来 Facebook 在地址簿中为其电子邮件联系人添加了奇怪的标签,因此当您查询标签时,它会返回一个 nil 值,这会给您带来错误的访问错误和崩溃

我个人在它周围放了一个 if 语句,只是忽略了所有 nil 地址,因为无论如何我只想要工作地址,请参阅代码中的以下示例(我没有检查过,但它应该可以工作)

在“分析”我之前的回答后,我注意到它实际上报告了内存泄漏,所以我将 if 移动了一点,并且更新的代码在它下面消失了。

for (int i=0; i<ABMultiValueGetCount(emailsRef); i++) {

    CFStringRef currentEmailLabel = ABMultiValueCopyLabelAtIndex(emailsRef, i);
    CFStringRef currentEmailValue = ABMultiValueCopyValueAtIndex(emailsRef, i);

    if (currentEmailLabel != nil) {

        if (CFStringCompare(currentEmailLabel, kABWorkLabel, 0) == kCFCompareEqualTo) {
            [self.contactInfoDict setObject:(__bridge NSString *)currentEmailValue forKey:@"workEmail"];
        }
        CFRelease(currentEmailLabel);
    }

    CFRelease(currentEmailValue);
}
CFRelease(emailsRef);//END OF IF WRAPPER

如果您想要这些电子邮件并且不在乎它们是 Facebook,我想您可以对标签进行 nil 检查并重新标记它,或者完全忽略标签比较。

关于iOS通讯录导入崩溃异常类型: EXC_BAD_ACCESS (SIGSEGV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24719058/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap