Detect requests from mobile devices in your Ember apps!
Works seamlessly in both the browser and in FastBoot!
This addon leverages isMobile.js for parsing user agent strings. It also exports isMobile.js as an ES6 accessible module.
ember install ember-is-mobile
The isMobile service is auto-injected into your app and provides access to the results of the user agent tests provided by isMobile.js. This service works in both the browser and in FastBoot. The FastBoot support is particularly useful if you want to conditionally render large blocks of content to target desktop or mobile devices.
N.B. Don't use this addon as a replacement for good responsive design!
You can query the user agent tests in your controllers, components and routes:
this.get('isMobile.any'); // => true|false
The properties are also available in templates:
The is-mobile helper can be used as an alternative to isMobile
service.
It expects one argument – a string specifying the user agent test.
The full list of user agent tests provided by isMobile:
any
phone
tablet
apple
android
amazon
windows
seven_inch
other
This addon also shims isMobile.js, so you can import it yourself if you need to.
In most cases you should use the service instead. If you need to use this addon as a shim only, open an issue and I'll consider a way of making the service injection opt-out.
import isMobile from 'ember-is-mobile';
In the browser, the isMobile object returns the computed user agent tests.
isMobile; // => { apple: {}, windows: {}, ... }
In FastBoot however, the import returns just the isMobile function, since navigator
is not available. You can fetch the FastBoot headers yourself and run the method manually. Note that this syntax will only work in FastBoot.
if (this.get('fastboot.isFastBoot')) {
const headers = this.get('fastboot.request.headers');
const userAgent = headers.get('user-agent');
isMobile(userAgent); // => { apple: {}, windows: {}, ... }
}
This addon uses a blueprint to add ismobilejs
to your app's dependencies using a blueprint. This step is necessary if you're using FastBoot. To get the latest version from the blueprint, run ember g ember-is-mobile
.
This project is licensed under the MIT License © Sander Melnikov.