Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect whether salesforce lightning app is running on a mobile browser or desktop browser?

We have salesforce lightning app which runs on both mobile and desktop. Need to write some code only for mobile application. How to detect whether app is running on a mobile browser or desktop browser? I used following code but its not working:

checkMobileBrowser: function(component){
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
        return true;        
    }else{
        return false;
    }
}
like image 497
Kitty Avatar asked Dec 02 '25 09:12

Kitty


2 Answers

The $Browser global value provider returns information about the hardware and operating system of the browser accessing the application.

Returns a FormFactor enum value based on the type of hardware the browser is running on.

DESKTOP for a desktop client PHONE for a phone including a mobile phone with a browser and a smartphone TABLET for a tablet client (for which isTablet returns true)

Ctrl

({
    checkBrowser: function(component) {
        var device = $A.get("$Browser.formFactor");
        alert("You are using a " + device);
    }
})
Component

<aura:component>
        {!$Browser.isTablet}
        {!$Browser.isPhone}
        {!$Browser.isAndroid}
        {!$Browser.formFactor}
    </aura:component>

By using this way you can determine

Resource: http://www.janbask.com/salesforce-lightning

like image 90
Daisy Scott Avatar answered Dec 04 '25 00:12

Daisy Scott


You can also use the $Browser global value provider:

function checkMobileBrowser(){ 
   return $A.get("$Browser.formFactor") !== "DESKTOP"
}

This will ensure that your detection matches what the application uses, if your component is ever embedded in S1 or SFX, and everything will switch on the same logic.

$Browser is also available in component markup:

<aura:if "{!$Browser.formFactor !== 'DESKTOP'}">
   <component/>
</aura:if> 

You might want to search the documentation for $Browser, because it allows for very granular hardware detection, and there might be something else for your specific use case.

https://resources.docs.salesforce.com/sfdc/pdf/lightning.pdf

like image 41
JF Paradis Avatar answered Dec 04 '25 00:12

JF Paradis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!