React Native Create Torch Flashlight Application Android iOS Example

Flashlight is a important part of every mobile phone in current arena. It comes build in with each multimedia smartphone that comes with camera. Flashlight is basically used as camera flash in mobile phones but it can be also used as individual flashlight at night. In react naive there is no direct build in component available to control the flashlight manually but using the react-native-torch NPM library we can easily control both android and iOS mobile phones flashlight and make them turn on and off. In this tutorial we would going to Create Torch Flashlight Application for both Android & iOS react native applications and turn on off the flashlight on button click. We can also run the flashlight after minimizing the application as background.

Note: To access the android mobile phone camera we need to approve the camera run time permission in android.

Contents in this project Create Torch Flashlight Application in Android iOS React Native App Example.

1. Open your react native project folder in CMD or Terminal and execute the below command to install the React Native Torch library.

Screenshot of CMD:


Screenshot of CMD after successfully installed the library:

2. After done installing the library we need to execute the link command in order to index the newly installed library in our current react native project.

Screenshot of CMD :

3. To access the Flashlight we need to first access the camera in our react native android app. So we need to authorize the CAMERA runtime permission.

Goto ReactNativeProject ->android ->app->src->main->AndroidManifest.xml file.

Put the below camera permission in it.

Now define the USES-SDK versions and save the file.

Source code of AndroidManifest.xml file after done above changes.

4. Open ReactNativeProject ->android ->app->build.gradle file. Change the targetSdkVersion to 23 like i did in below example, This step is very important.

Source code of build.gradle file :

5. Import Platform, StyleSheet, View, PermissionsAndroid, Text, Alert and TouchableOpacity component in your react native project’s App.js file.

6. Import Torch component from react-native-torch library in your react native project.

7. Create a ASYNC function named as request_camera_runtime_permission(). This function is used to request the camera runtime permission in android mobile phones. If you want to learn more about Requesting Runtime permission then you can read my this tutorial.

8. Create the ASYNC componentDidMount() method in your main App’s export default class. We would call the request_camera_runtime_permission() method using await.

9. Create 2 function named as on_Torch() and off_Torch() to turn on and off the mobile phone flashlight.

10. Creating 2 buttons in render’s return block using Touchable Opacity.

11. Creating Style.

12. Complete source code for App.js File :

Screenshots:

React Native Create Torch Flashlight Application Android iOS Example