菜鸟教程小白 发表于 2022-12-13 08:32:27

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


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

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

<pre><code>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:
</code></pre>

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

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

<pre><code>-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
;
}

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
_arrContactsData =[ init];
NSMutableDictionary *contactInfoDict = [
                                        initWithObjects:@[@&#34;&#34;, @&#34;&#34;, @&#34;&#34;, @&#34;&#34;, @&#34;&#34;, @&#34;&#34;, @&#34;&#34;, @&#34;&#34;, @&#34;&#34;]
                                        forKeys:@[@&#34;firstName&#34;, @&#34;lastName&#34;, @&#34;mobileNumber&#34;, @&#34;homeNumber&#34;, @&#34;homeEmail&#34;, @&#34;workEmail&#34;, @&#34;address&#34;, @&#34;zipCode&#34;, @&#34;city&#34;]];

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

// Get the first name.
if (generalCFObject) {
    ;
    CFRelease(generalCFObject);
}

// Get the last name.
generalCFObject = ABRecordCopyValue(person, kABPersonLastNameProperty);
if (generalCFObject) {
    ;
    CFRelease(generalCFObject);
}

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

    if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
      ;
    }

    if (CFStringCompare(currentPhoneLabel, kABHomeLabel, 0) == kCFCompareEqualTo) {
      ;
    }

    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&lt;ABMultiValueGetCount(emailsRef); i++) {
    CFStringRef currentEmailLabel = ABMultiValueCopyLabelAtIndex(emailsRef, i);
    CFStringRef currentEmailValue = ABMultiValueCopyValueAtIndex(emailsRef, i);

    if (CFStringCompare(currentEmailLabel, kABHomeLabel, 0) == kCFCompareEqualTo) {
      ;
    }

    if (CFStringCompare(currentEmailLabel, kABWorkLabel, 0) == kCFCompareEqualTo) {
      ;
    }

    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) &gt; 0) {
    NSDictionary *addressDict = (__bridge NSDictionary *)ABMultiValueCopyValueAtIndex(addressRef, 0);

    forKey:@&#34;address&#34;];
    forKey:@&#34;zipCode&#34;];
    forKey:@&#34;city&#34;];
}
CFRelease(addressRef);


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

    ;
}

// Initialize the array if it&#39;s not yet initialized.
if (_arrContactsData == nil) {
    _arrContactsData = [ init];
}
// Add the dictionary to the array.
;

// Reload the table view data.


// Dismiss the address book view controller.
;
NSDictionary *info = ;
self.txfFirstName.text = ;
self.txfLastName.text = ;
self.txfMobile.text =;
self.txfEmail.text =;
NSLog(@&#34;Info %@&#34;,info);

return NO;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>看起来 Facebook 在地址簿中为其电子邮件联系人添加了奇怪的标签,因此当您查询标签时,它会返回一个 nil 值,这会给您带来错误的访问错误和崩溃</p>

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

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

<pre><code>for (int i=0; i&lt;ABMultiValueGetCount(emailsRef); i++) {

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

    if (currentEmailLabel != nil) {

      if (CFStringCompare(currentEmailLabel, kABWorkLabel, 0) == kCFCompareEqualTo) {
            ;
      }
      CFRelease(currentEmailLabel);
    }

    CFRelease(currentEmailValue);
}
CFRelease(emailsRef);//END OF IF WRAPPER
</code></pre>

<p>如果您想要这些电子邮件并且不在乎它们是 Facebook,我想您可以对标签进行 nil 检查并重新标记它,或者完全忽略标签比较。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS通讯录导入崩溃异常类型: EXC_BAD_ACCESS (SIGSEGV),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/24719058/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/24719058/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS通讯录导入崩溃异常类型: EXC_BAD_ACCESS (SIGSEGV)