As the first step, install this module:
npm install --save https://github.com/willnaoosmit/react-native-keep-visible.git
- In XCode, in the project navigator, right click
Libraries➜Add Files to [your project's name] - Go to
node_modules➜react-native-keep-awakeand addKCKeepAwake.xcodeproj - In XCode, in the project navigator, select your project. Add
libKCKeepAwake.ato your project'sBuild Phases➜Link Binary With Libraries - Click
KCKeepAwake.xcodeprojin the project navigator and go theBuild Settingstab. Make sure 'All' is toggled on (instead of 'Basic'). In theSearch Pathssection, look forHeader Search Pathsand make sure it contains both$(SRCROOT)/../../react-native/Reactand$(SRCROOT)/../../../React- mark both asrecursive.
- Open up `android/app/src/main/java/[...]/MainApplication.java (React Native 0.29+)
- Add
import com.corbt.keepawake.KCKeepAwakePackage;to the imports at the top of the file - Add
new KCKeepAwakePackage()to the list returned by thegetPackages()method
- Append the following lines to
android/settings.gradle:
include ':react-native-keep-awake'
project(':react-native-keep-awake').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-keep-awake/android')- Insert the following lines inside the dependencies block in
android/app/build.gradle:
compile project(':react-native-keep-awake')There are two ways to use this package: by rendering it as a component, or by explicitly calling
the KeepAwake.activate() and KeepAwake.deactivate() static methods. Both are demonstrated below.
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import KeepAwake from 'react-native-keep-awake';
// Method 1
class MyComponent extends Component {
render() {
if (this.props.screenShouldBeAwake) {
return (
<View>
<Text>Screen will be kept awake</Text>
<KeepAwake />
</View>
)
} else {
return (
<View>
<Text>Screen can sleep</Text>
</View>
);
}
}
}
// Method 2
function changeKeepAwake(shouldBeAwake) {
if (shouldBeAwake) {
KeepAwake.activate();
} else {
KeepAwake.deactivate();
}
}In method 1, the screen will be locked awake when the <KeepAwake /> component is mounted, and the lock will be disabled when the component is unmounted. Method 2 gives you more explicit control of when the lock is active or not.