将电话号码和电子邮件添加到地址簿会导致iPhone App崩溃

时间:2012-01-03 12:27:11

标签: iphone objective-c ios abaddressbook

ABRecordSetValue(person, kABPersonPhoneProperty, (__bridge CFTypeRef)phoneNum, nil);
ABRecordSetValue(person, kABPersonEmailProperty, (__bridge CFTypeRef)eMailId, nil);

评论这两行提供了正确的输出(将条目保存到AdressBook,无需电子邮件和电话输入)。 但是,在执行过程中它会失败并导致程序崩溃。

Vars的价值是:

phoneNum :000-000-0000

eMailId :AbcdefghIjklm@qwertyuiopasdfghjk.com

错误类型:EXC_BAD_ACCESS

任何想法? 或者还需要更多信息??

2 个答案:

答案 0 :(得分:16)

很高兴看到您定义变量的位置和类型。您可能还在这些行之前发布了地址簿。 (例如CFRelease(multiPhone);) 据我所知,phoneNumemailID应为ABMutableMultiValueRef。至少我是这样做的:

添加电话号码

ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);

ABMultiValueAddValueAndLabel(multiPhone, [self.contact telephone], kABPersonPhoneMobileLabel, NULL);            
ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone,nil);

添加电子邮件:

    ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiEmail, @"abc@abc.com", kABWorkLabel, NULL);
    ABRecordSetValue(person, kABPersonEmailProperty, multiEmail, &error);
    CFRelease(multiEmail);

有关Address Book tutorial

的更多信息

希望这有帮助。

答案 1 :(得分:3)

试一下

ABAddressBookRef addressBook = ABAddressBookCreate(); 

ABRecordRef person = ABPersonCreate(); 


ABMutableMultiValueRef phoneNumberMultiValue 

=ABMultiValueCreateMutable(kABPersonPhoneProperty);

//phoneNumber is the number to be save in Address Book


ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,phoneNumber,kABPersonPhoneMobileLabel, NULL);

//EmailId is the emailId to be save in Address Book

ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,EmailId,kABPersonPhoneMobileLabel, NULL);

ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue,nil); 

// set the phone number property

ABAddressBookAddRecord(addressBook, person, nil); 

ABAddressBookSave(addressBook, nil); 

CFRelease(person); 

我希望它可能会有所帮助......