这些是对您的代码的更正
NSString *theEmailAddressWeWantToObtain = @"";
ABPerson *aPerson = [[ABAddressBook sharedAddressBook] me];
ABMultiValueRef *emails = [aPerson valueForProperty:kABEmailProperty]; //No such thing as ABMultiValue; it's ABMultiValueRef
if(ABMultiValueGetCount(emails) > 0) //"emails" is not an array, so you can't use the "count" method
theEmailAddressWeWantToObtain = [emails valueAtIndex:0];
我对键值编码不太熟悉,所以我不确定你的方法与此相关。
这是我会做的方式
电子邮件中存储了三个电子邮件地址ABMultiValueRef
:家庭、工作和其他电子邮件。试试这个代码来获取家庭电子邮件:
NSString *email;
ABRecordRef currentPerson = (__bridge ABRecordRef)[[PSAddressBook arrayOfContacts] objectAtIndex:identifier];
ABMultiValueRef emailsMultiValueRef = ABRecordCopyValue(currentPerson, kABPersonEmailProperty);
NSUInteger emailsCount;
//Goes through the emails to check which one is the home email
for(emailsCount = 0; emailsCount <= ABMultiValueGetCount(emailsMultiValueRef);emailsCount++){
NSString *emailLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex (emailsMultiValueRef, emailsCount);
if([emailLabel isEqualToString:@"Home"]){
if ((__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emailsMultiValueRef, emailsCount) != NULL){
email = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonEmailProperty);
}
//If the last name property does not exist
else{
email = @"NULL";
}
}
}
CFRelease(emailsMultiValueRef);
如果您对代码有任何疑问,请在评论中提问。希望这可以帮助!
编辑:
代码中提到的PSAddressBook
类可以在这里找到:https ://github.com/pasawaya/PSAddressBook