4

I'm developing a lightning app that users will be accessing via desktop and through Salesforce1. I've been using the $Browser variable to detect if the user is coming from mobile but that only works if they are accessing the page via a web browser on their phone and not the SF1 mobile app. It seems the $User.getTheme and $UserInfo.getUiTheme() functionality only works in visualforce pages.

1 Answer 1

4

There is an apex version of same as well

public with sharing class UIThemeController {
 @AuraEnabled
 public static String getUIThemeDescription() {
    String theme = UserInfo.getUiThemeDisplayed();
    return theme;
 }
}

The lightning component code will look like below

<aura:component implements="flexipage:availableForAllPageTypes" controller="UIThemeController" access="global">
 <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
 <aura:attribute name="Name" type="String" />
 <aura:attribute name="isLEX" type="Boolean" />
 <aura:attribute name="Desc" type="String" />
 <div class="slds">
  <div class="slds-card slds-card--empty">
  <div class="slds-card__header slds-grid grid--flex-spread">
   <h2 class="slds-text-heading--small slds-truncate">Lightning Component</h2>
  </div>
 <div class="slds-card__body slds-p-horizontal--small">
   <h3 class="slds-text-heading--small slds-p-top--large slds-p-bottom--large">Detected Theme: {!v.Name}</h3>
  <h2 class="slds-text-heading--small slds-p-top--large slds-p-bottom--large">{!v.Desc}</h2>
<aura:if isTrue="{!v.isLEX}">
 <p>This text is only displayed in Lightning Experience</p>
 <aura:set attribute="else">
 <p>This text is only displayed outside of Lightning Experience</p>
</aura:set>

2
  • 1
    I don't have enough rep to mark your answer correct Mohith, but that works perfectly. Thank you!
    – ChrisLuman
    Jun 30, 2016 at 21:22
  • Welcome to SFSE @ChrisLuman! Only the person who posted the question can choose an accepted answer (not even mods can change it) but participate and you'll soon be able to upvote answers!
    – Matt Lacey
    Jul 1, 2016 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.