【发布时间】:2020-08-12 14:20:13
【问题描述】:
我正在使用 React Native 开发一个移动应用程序,我想在用户点击时打开移动设备的设置 strong> 在特定按钮上。
我浏览了 RN 的 docs,但仍然找不到任何可以帮助我实现相同目标的方法。有人可以指导我吗?如何实施?
谢谢:)
【问题讨论】:
标签: android ios react-native settings
我正在使用 React Native 开发一个移动应用程序,我想在用户点击时打开移动设备的设置 strong> 在特定按钮上。
我浏览了 RN 的 docs,但仍然找不到任何可以帮助我实现相同目标的方法。有人可以指导我吗?如何实施?
谢谢:)
【问题讨论】:
标签: android ios react-native settings
作为对Rafael Perozin 答案的补充。
在 Android 上,您还可以通过 Linking 发送 Intent。
import { Button, Linking } from "react-native";
const Screen = () => (
<Button
title="Open Settings"
onPress={() => {
Platform.OS === 'ios'
? Linking.openURL('App-Prefs:Bluetooth')
: Linking.sendIntent("android.settings.BLUETOOTH_SETTINGS");
}}
/>
);
【讨论】:
就我而言,我想将用户发送到蓝牙设置,而CR7 的答案对我不起作用。
所以,我解决了这个问题:
Linking 来自 react-native
AndroidOpenSettings 来自 react-native-android-open-settings
我还使用react-native 中的Platform 来检查当前设备是否为IOS。
查看代码:
...
import {Linking, Platform} from 'react-native';
import AndroidOpenSettings from 'react-native-android-open-settings';
...
...
const goToSettings = () =>
Platform.OS === 'ios'
? Linking.openURL('App-Prefs:Bluetooth')
: AndroidOpenSettings.bluetoothSettings();
...
...
return (
<TouchableOpacity onPress={() => goToSettings()}>
<Text>Go to settings</Text>
</TouchableOpacity>
);
...
【讨论】:
您是否尝试过使用 Linking 组件形式 react-native (v0.60+)?
import { Button, Linking } from "react-native";
const Screen = () => (
<Button
title="Open Settings"
onPress={() => {
Linking.openSettings();
}}
/>
);
【讨论】: