10

REACTNATIVE : Currently using Linking.openSettings() to open my application settings page.

Now, I want to open the app permission screen directly, which is inside the App settings to enable permissions like Location, Storage etc., in both Android and iOS using REACT NATIVE.

Any possibilities? Thanks in advance!

Eshack Nazir
  • 354
  • 3
  • 16

2 Answers2

6

Shortly it's possible,

For IOS operating system, Try below,

import { Linking } from 'react-native'
Linking.openURL('app-settings:')

But Android not working with Linking,We should add two dependency,

npm install --save react-native-device-info

npm install react-native-intent-launcher

As a result,

import DeviceInfo from 'react-native-device-info';
import IntentLauncher, { IntentConstant } from 'react-native-intent-launcher'
const package= DeviceInfo.getBundleId();
const openAppSettings = () => {
  if (Platform.OS === 'ios') {
    Linking.openURL('app-settings:')
  } else {
    IntentLauncher.startActivity({
      action: 'android.settings.APPLICATION_DETAILS_SETTINGS',
      data: 'package:' + package
    })
  }
}
Onur Ozdemir
  • 86
  • 1
  • 4
0

ANDROID ONLY:

I found a solution based on this answer from Android devs; and then I tried with React Native.

I use the react-native-permissions library for this purpose.

With that in mind, we must first request 'android.permission.ACCESS_FINE_LOCATION permission, so that we can redirect our user to ask for Background location, which the user can only change directly in the App settings.

My app has one screen asking for the android.permission.ACCESS_FINE_LOCATION. Then, on the next screen, I request android.permission.ACCESS_FINE_LOCATION permission.

In React Native, the code will be the following:

import { Platform } from 'react-native';
import { PERMISSIONS, RESULTS, check, request } from 'react-native-permissions';


if (Platform.OS === 'android') {
  // first ask the FINE_LOCATION permission, and then check
  const androidLocationPermissionRequest = await request(
    PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
  );
  const isGranted = androidLocationPermissionRequest === RESULTS.GRANTED;

  if (isGranted) {
    // This function will redirect the user to YOUR APP "Location Permission" page
    // instead of showing the prompt (default behaviour) because it is how Android currently works to give bg location.
    // so the user can change to "Always allow" etc.
    await request(PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION);
  }
}
Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53