Getting user's default email address in Cocoa
Asked Answered
E

2

12

How do I obtain the user's default email address? I need to obtain it for my crash reporter dialog, so the user won't have to fill it out manually.

Expecting answered 3/2, 2011 at 23:49 Comment(0)
E
17

Never mind, I got it. First, I just have to add AddressBook.framework into my Linked Frameworks. Then, this is the code required:

#import <AddressBook/AddressBook.h>

NSString *theEmailAddressWeWantToObtain = @"";
ABPerson *aPerson = [[ABAddressBook sharedAddressBook] me];
ABMultiValue *emails = [aPerson valueForProperty:kABEmailProperty];
if([emails count] > 0)
  theEmailAddressWeWantToObtain = [emails valueAtIndex:0];
Expecting answered 4/2, 2011 at 1:25 Comment(5)
You may want to use a combo box and populate its pop-up menu with all of the user's email addresses.Instate
For iOS 6.1 Xcode doesn't like ABPerson *aPerson = [[ABAddressBook sharedAddressBook] me];. The protocol must have changed drastically since 2011.Nerin
"me" is only a property on OS X, not iOSNonrecognition
Does that mean you can't get the users email on iOS?Nalda
terrible, to be able to autocomplete user's email we should ask for access to address book...Lilililia
S
1

From "*Address Book Programming Guide for iOS":

Link the Address Book UI and Address Book frameworks to your project.

Important The project will fail to build (with a linker error) if you do not link against both of these framework.

Linking in the Framework without the UI will prevent the sample code from compiling.

Scow answered 29/4, 2012 at 18:49 Comment(1)
The above poster is suggesting that in any project using the Address Book Framework, you should also include Address Book UI.Radarscope

© 2022 - 2024 — McMap. All rights reserved.