How to get the email of the currently logged in Salesforce user

Submitted by John on Fri, 09/06/2019 - 16:16

Earlier today I spent quite a bit of time trying to figure out how to get the email of the currently logged in Salesforce user. Specifically, using the SalesforceSDK in iOS, using Objective-C.

You'd think this would be easy. Just look at the documentation! Alas! Salesforce has not decided to invest much into the consistency and comprehensiveness of their documentation.

Okay. Google! Surely Google will have the answer!

Alas again! There are a zillion Salesforce frameworks out there! React, Swift, Objective-C, Android/Java, something called "Apex", whatever that is. All of these platforms do the same things, but in a slightly different way. So for a generic query, like "how do I get the email of the currently logged in user in Salesforce?" you'll have to wait

So add "Objective-C" to the query!

The final Alas! is that Salesforce has changed how to do this half a dozen times over the past 3-5 years. So you'll find solutions on how to do it with SFAccountManager, how to do it with SFAuthenticationManager, etc. This was a huge rabbit hole.

Eventually I found a method using SFUserAccountManager... although it too was deprecated! At least there were some Xcode hints on what the correct answer might look like however.

Finally, here's what works in Objective-C using MobileSDK 7.2:

[[SFUserAccountManager sharedInstance] userAccountForUserIdentity:[[SFUserAccountManager sharedInstance] currentUserIdentity]]

The email is in the idData object. There are a number of other good properties in there, like lastName, firstName, langauge, locale, all of your user info is going to be there.

Hope this helps!