iOS : Can't get the user email address
Asked Answered
P

1

6

I want to get the user mail address, as shown in this thread : Getting user's default email address in Cocoa

But when I tried :

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

I have these errors :

  • Use of undeclared identifier 'aPerson'
  • Use of undeclared identifier 'ABAddressBook'
  • Unknown type 'ABMultiValue'

I've linked AddressBook and AddressBookUI, and imported AddressBook/AddressBook.h

What's wrong ?

Psychro answered 27/6, 2012 at 15:36 Comment(0)
R
7

These are the corrections to your code

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];

I'm not so familiar with Key Value Coding, so I'm not sure about your methods related to that.

This is the way I would do it

There are three email address stored in the email ABMultiValueRef: home, work, and other emails. Try this code to get the home email:

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);

If you have any questions about the code, just ask in the comments. Hope this helps!

EDIT:

The PSAddressBook class mentioned in the code can be found here: https://github.com/pasawaya/PSAddressBook

Reeding answered 27/6, 2012 at 16:20 Comment(9)
Using the predefined constants when comparing the label is probably safer: if (CFStringCompare(emailLabel, kABHomeLabel, 0) == kCFCompareEqualTo) (your email label should be a CFStringRef and not be cast to NSString, though)Actor
Sorry, but i get "Job appears to have crashed: Segmentation fault: 11": at first time, String is @"Home" like @"!$<Home>$!", and on the next iteration except this error at line: (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex (emailsMultiValueRef, emailsCount)Enucleate
What is this PSAddressBook and 'identifier' ?Yammer
@Ujwal - Sorry. That's a class I made on my own. You can find it here: <github.com/pasawaya/PSAddressBook>Reeding
page not found on your linkPhotofinishing
@Photofinishing -That's weird. I'll try to fix that.Reeding
@Ashwini - I took it down from GitHub when I was cleaning up my page, but I can post the code for the methods you want. What code would you want?Reeding
I need method arrayOfContacts in order to get above code snippet working.Spaceless
@Ashwini - +(NSArray *)arrayOfContacts{ ABAddressBookRef addressBook = ABAddressBookCreate(); NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); CFRelease(addressBook); return arrayOfPeople; }Reeding

© 2022 - 2024 — McMap. All rights reserved.