我想获得用户邮件地址,如下面的线程所示:Getting user's default email address in Cocoa
但当我试着:
NSString *theEmailAddressWeWantToObtain = @"";
ABPerson *aPerson = [[ABAddressBook sharedAddressBook] me];
ABMultiValue *emails = [aPerson valueForProperty:kABEmailProperty];
if([emails count] > 0)
theEmailAddressWeWantToObtain = [emails valueAtIndex:0];我有这样的错误:
我链接了AddressBook和AddressBookUI,并导入了AddressBook/AddressBook.h
怎么了?
发布于 2012-06-27 16:20:02
--以下是对代码的更正
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
https://stackoverflow.com/questions/11229967
复制相似问题