我正在四处寻找如何在我的应用程序启动时检查设备上是否启用了网络或GPS.如果它们被禁用,则提示用户启用它.
这在React-native中是否可行?是否有任何类或工具可以帮助这种类型的对话框?
所以我将开始回答我自己的问题.
对于GPS:
似乎有一个明智的解决方案.如果存在地理定位请求,IOS似乎本地请求.而对于Android,这不是原生支持,但有人为此创建了一个模块(https://github.com/webyonet/react-native-android-location-services-dialog-box)
所以在我的动作创建者中我添加了下一个代码:
if(Platform.OS === 'android') LocationServicesDialogBox.checkLocationServicesIsEnabled({ message: "Use Location?
\ This app wants to change your device settings:
\ Use GPS for location
", ok: "YES", cancel: "NO" }).then(() => { locationTracking(dispatch, getState, geolocationSettings) })
对于网络:没有本机支持,所以我最终做我自己的行动创建者来检查.
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.
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) } }
为了在Android上启用location / gps,我可以推荐以下模块:https : //github.com/Richou/react-native-android-location-enabler
它使用标准的Android对话框进行定位: