react-native 检查并提示用户启用网络/GPS [英] react-native check and prompt user to enable Network/GPS
问题描述
我正在查看如何在我的应用程序启动时检查设备上是否启用了网络或 GPS.如果它们被禁用,则提示用户启用它.
I am looking around on how to check if network or GPS are enabled on a device when my application starts. And if they are disabled prompt the user to enable it.
这在 React-native 中可行吗?是否有任何类或工具可以帮助处理此类对话框?
Is this posible in React-native?, and is there any class or tool that could help with this type of dialogs?
推荐答案
所以我要开始回答我自己的问题了.
So I am going to start answering my own question.
对于 GPS:
似乎有一个明智的解决方案.如果有地理定位请求,IOS 似乎会在本机请求.对于 Android,这不是本机支持的,但有人为此创建了一个模块( https://github.com/webyonet/react-native-android-location-services-dialog-box )
there seems to be a sensible solution. IOS seems to natively request if there is a geolocation request. And for Android this is not natively supported but someone has created a module for that ( https://github.com/webyonet/react-native-android-location-services-dialog-box )
所以在我的动作创建器中我添加了下一个代码:
so in my action creator I added the next code:
if(Platform.OS === 'android')
LocationServicesDialogBox.checkLocationServicesIsEnabled({
message: "<h2>Use Location?</h2> \
This app wants to change your device settings:<br/><br/>\
Use GPS for location<br/><br/>",
ok: "YES",
cancel: "NO"
}).then(() => {
locationTracking(dispatch, getState, geolocationSettings)
})
对于网络:两者都没有本地支持,所以我最终自己做动作创建者来检查.
For Network: There is no native support for neither so i end up doing my own action creator to check.
export function networkCheck(){
return (dispatch) => {
const dispatchNetworkState = (isConnected) => dispatch({
type: types.NETWORK_STATE,
state: isConnected
})
const handle = () => NetInfo.isConnected.fetch().done(dispatchNetworkState)
NetInfo.isConnected.addEventListener('change', handle);
}
}
一点额外的:
对于 GPS,我添加了这个来检查用户是否在任务中间去禁用 GPS.
for GPS i added this to check if the user goes and disable GPS on the middle of the task.
export function locationCheck(geolocationSettings = {enableHighAccuracy: true, timeout: 20000, maximumAge: 10000, distanceFilter:10}){
return (dispatch) => {
navigator.geolocation.watchPosition(
() => {
dispatch({
type: types.LOCATION_STATE,
state: true
})
},
() => {
dispatch({
type: types.LOCATION_STATE,
state: false
})
},
geolocationSettings)
}
}
这篇关于react-native 检查并提示用户启用网络/GPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!