lightning component

How To Detect a Device In Salesforce Lightning Component ?

Lightning Component

Sharing is caring!

In some of our use cases, we might have to know the device that user is in. This post explains how we can detect a device in Lightning Component.

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

{!$Browser.isTablet}      > To detect the tablet device
{!$Browser.isPhone}      > To detect the user is in Mobile Phone / mobile browser
{!$Browser.isAndroid}   > To detect the user is in Android device
{!$Browser.formFactor} > 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)

Example:

Create a new lightning component : DetectMyDevice.cmp

Create a Lightning Application (DetectMyDeviceApp.app)

Output:

lightning component

To detect a device in JavaScript controller:

var device = $A.get("$Browser.formFactor");

Check Also : How to Use Design Resource In Lightning Component Bundle?

Resources:

Lightning Developer Guide

About The contributor: 

Sharing is caring!

(Visited 815 times, 8 visits today)

2 comments

Leave a Reply