首先简要介绍一下背景,我聘请了一名开发人员来构建我的应用程序,但我知道通过剖析现有代码可以更好地学习,因此我的目标是通过这种方式了解更多有关 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;
}
看起来 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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |