iOS:无法获取用户电子邮件地址。

6

我希望获取用户的电子邮件地址,就像这个帖子中所示:在Cocoa中获取用户默认电子邮件地址

但是当我尝试:

NSString *theEmailAddressWeWantToObtain = @"";
ABPerson *aPerson = [[ABAddressBook sharedAddressBook] me];
ABMultiValue *emails = [aPerson valueForProperty:kABEmailProperty];
if([emails count] > 0)
        theEmailAddressWeWantToObtain = [emails valueAtIndex:0];

我有以下错误:

  • 使用未声明的标识符'aPerson'
  • 使用未声明的标识符'ABAddressBook'
  • 未知类型'ABMultiValue'

我已经链接了AddressBook和AddressBookUI,并导入了AddressBook/AddressBook.h。

出了什么问题?

1个回答

7
这是你代码的更正建议。
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


抱歉,但我第一次遇到的错误是“作业似乎已经崩溃:分段错误:11”。字符串最初为@“Home”,类似于@“_!$<Home>$!_”,在下一次迭代中除了这个错误之外,在行(__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(emailsMultiValueRef, emailsCount)。 - HotJard
@Ujwal - 抱歉,那是我自己创建的一个类。你可以在这里找到它:https://github.com/pasawaya/PSAddressBook。 - pasawaya
@Ashwini - 当我整理我的页面时,我从GitHub上删除了它,但我可以发布你想要的方法的代码。你需要哪些代码? - pasawaya
我需要方法arrayOfContacts才能使上面的代码片段正常工作。 - Ashwini Shahapurkar
@Ashwini - +(NSArray *)arrayOfContacts{ ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); CFRelease(addressBook); return arrayOfPeople; }@阿什温尼 - +(NSArray *)arrayOfContacts { ABAddressBookRef addressBook = ABAddressBookCreate(); NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); CFRelease(addressBook); 返回arrayOfPeople; }
- pasawaya
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接