How to detect if a user is in Salesforce1 vs Lightning Desktop

With Spring 16 release, we are getting new values added to the existing $User.UITheme Global variable and to the UserInfo.getUiTheme() method which will help us to identify the current user's UI mode.

here's the list of possible values

  • Theme1 — Obsolete Salesforce theme
  • Theme2 — Salesforce Classic 2005 user interface theme
  • Theme3 — Salesforce Classic 2010 user interface theme
  • Theme4d — Modern “Lightning Experience” Salesforce theme
  • Theme4t — Salesforce1 mobile Salesforce theme
  • PortalDefault — Salesforce Customer Portal theme
  • Webstore — Salesforce AppExchange theme

hopefully we can use this in Workflows, Validations, Formulas, Apex & Visualforce..

Official Reference

Until there's an official Salesforce way I'm going with a JS solution to detect if the user is using a mobile device. I'm using JS to find the user agent and match against the keyword 'Mobi':

var isMobile = function() {
     //using regex here so developers can add more keywords if needed
     return navigator.userAgent.match('(Mobi)') ? true : false;

If the user is using a browser on a mobile device, they will be navigated to Salesforce1. Also the User Agent for the Salesforce1 App (Android and iOS) has 'SalesforceMobileSDK'.

Thanks Vamsi Krishna for pointing me in the right direction.