首先是一个快速的背景,我聘请了一个开发人员来构建我的应用程序但是我知道我通过剖析现有代码来学习更好,所以我的目标是以这种方式学习更多有关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:
研究告诉我,应用程序试图调用它无法访问的内容,但不确定从何处开始。
以下是我认为导致问题的代码区域。 (这个动作)。
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
[_addressBookController dismissViewControllerAnimated:YES completion:nil];
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)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 objectForKey:(NSString *)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;
}
答案 0 :(得分:4)
看起来Facebook在地址簿中将其电子邮件联系人标记为奇怪,因此当您查询标签时,它会返回一个nil值,这将为您提供错误的访问错误和崩溃
个人我在它周围放了一个if语句并且忽略了所有的nil地址,因为我只想要工作地址,请参阅代码中的以下示例(我没有检查过这个但它应该可以工作)在“分析”我之前的回答之后,我注意到它实际上报告了内存泄漏,所以我移动了一点点,并且更新后的代码消失了。
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,我想您可以在标签上进行零检查并重新标记它,或者完全忽略标签比较。