如何直接在React Native中打开应用程序的位置权限设置

问题描述 投票:0回答:2

REACTNATIVE : 目前使用 Linking.openSettings() 打开我的应用程序设置页面。

现在,我想直接打开应用程序权限屏幕,该屏幕位于应用程序设置内,以使用 REACT NATIVE 在 Android 和 iOS 中启用位置、存储等权限。

有什么可能吗?预先感谢!

react-native permissions settings react-native-navigation
2个回答
6
投票

很快就有可能了,

对于IOS操作系统,请尝试以下,

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

但是Android无法使用Linking,我们应该添加两个依赖项,

npm install --保存react-native-device-info

npm 安装react-native-intent-launcher

结果,

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
    })
  }
}

0
投票

仅限安卓:

我找到了一个基于Android开发人员的这个答案的解决方案;然后我尝试使用 React Native。

我使用

react-native-permissions
库来实现此目的。

考虑到这一点,我们必须首先请求

'android.permission.ACCESS_FINE_LOCATION
权限,以便我们可以重定向用户以询问后台位置,用户只能直接在应用程序设置中更改。

我的应用程序有一个屏幕要求

android.permission.ACCESS_FINE_LOCATION
。然后,在下一个屏幕上,我请求
android.permission.ACCESS_FINE_LOCATION
许可。

在 React Native 中,代码如下:

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);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.