🔦 A NativeScript flashlight plugin for Android and iOS
Find a file
2018-06-04 15:53:13 +02:00
demo Upgrading demo to {N} 2.3 2016-10-04 08:45:55 -04:00
platforms/android Let's try to register this as a NativeScript plugin 2015-07-02 09:20:05 -04:00
.gitignore bump 2017-11-01 15:57:52 +01:00
.npmignore bump 2017-11-01 15:57:52 +01:00
flashlight-common.js bump 2017-11-01 15:57:52 +01:00
flashlight.android.js Merge branch 'master' into master 2018-06-04 15:53:13 +02:00
flashlight.ios.js Add a brightness intensity feature for iOS 2016-09-17 23:09:31 +02:00
index.d.ts Add a brightness intensity feature for iOS 2016-09-17 23:09:31 +02:00
LICENSE Defining this module as an npm module 2015-03-17 08:58:28 -04:00
package.json bump 2017-11-01 15:57:52 +01:00
README.md bump 2017-11-01 15:57:52 +01:00

NativeScript Flashlight Plugin

A plugin for using your device's flashlight in NativeScript Android and iOS apps.

Installation

Run the following command from the root of your project:

$ tns plugin add nativescript-flashlight

This command automatically installs the necessary files, as well as stores nativescript-flashlight as a dependency in your project's package.json file.

Usage

To use the flashlight module you must first require() it:

// JavaScript
var flashlight = require("nativescript-flashlight");

Or if youre using TypeScript, import the module:

// TypeScript
import * as flashlight from "nativescript-flashlight";

After you have a reference to the module you can then call its on(), off(), and toggle() methods. For example, the code below turns your device's flashlight on with an intensity setting of 25%.

The optional intensity is supported only on iOS and is by default 1.0, which is 100% brightness.

// my-page.js
var flashlight = require("nativescript-flashlight");
flashlight.on({
    intensity: 0.25
});

In most cases you'll want to wrap your on() call with a check of isAvailable(), to handle devices where a flashlight is not available:

// my-page.js
var flashlight = require("nativescript-flashlight");
if (flashlight.isAvailable()) {
	flashlight.on();
} else {
	alert("A flashlight is not available on your device.");
}

Examples

The code below creates a button that toggles the device's flashlight:

<!-- my-page.xml -->
<Page loaded="pageLoaded">
    <StackLayout>
        <Button text="{{ flashlightState }}" tap="{{ toggleFlashlight }}" />
    </StackLayout>
</Page>
// my-page.js
var flashlight = require("nativescript-flashlight");
var observable = require("data/observable");
var viewModel = new observable.Observable();

// Set the initial text of the button
viewModel.set("flashlightState", "Turn on");

// A tap handle for the page's button. Toggle the state of the flashlight
// and the button's text
viewModel.toggleFlashlight = function() {
    if (flashlight.isAvailable()) {
        flashlight.toggle({
            intensity: 0.6 // optional, supported on iOS only (default: 1.0 which is 100% brightness)
        });
        viewModel.set("flashlightState", (flashlight.isOn() ? "Turn off" : "Turn on"));
    } else {
        alert("A flashlight is not available on your device.");
    }
};

function pageLoaded(args) {
    var page = args.object;
    page.bindingContext = viewModel;
}

exports.pageLoaded = pageLoaded;