React Native:如何在 Android 和 iOS 中打开 OS 设置?
React Native: How to open OS settings in Android and iOS?
我正在使用 React Native 开发一个 mobile 应用程序,我想在其中打开移动设备的 settings 当用户 单击 特定按钮时。
我已经浏览了 RN 的 docs,但仍然找不到任何可以帮助我实现相同目标的东西。有人可以指导我,如何实施?
谢谢:)
您是否尝试使用链接组件形式的 react-native (v0.60+)?
import { Button, Linking } from "react-native";
const Screen = () => (
<Button
title="Open Settings"
onPress={() => {
Linking.openSettings();
}}
/>
);
就我而言,我想将用户发送至 蓝牙设置,但 答案对我不起作用。
所以,我使用以下方法解决了问题:
- IOS:
Linking
来自 react-native
- ANDROID:
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>
);
...
作为 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");
}}
/>
);
我正在使用 React Native 开发一个 mobile 应用程序,我想在其中打开移动设备的 settings 当用户 单击 特定按钮时。
我已经浏览了 RN 的 docs,但仍然找不到任何可以帮助我实现相同目标的东西。有人可以指导我,如何实施?
谢谢:)
您是否尝试使用链接组件形式的 react-native (v0.60+)?
import { Button, Linking } from "react-native";
const Screen = () => (
<Button
title="Open Settings"
onPress={() => {
Linking.openSettings();
}}
/>
);
就我而言,我想将用户发送至 蓝牙设置,但
所以,我使用以下方法解决了问题:
- IOS:
Linking
来自react-native
- ANDROID:
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>
);
...
作为 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");
}}
/>
);