美文网首页
uniapp提示用户开启定位,跳转到开启定位页面

uniapp提示用户开启定位,跳转到开启定位页面

作者: 一城山水一成伤 | 来源:发表于2022-06-22 18:24 被阅读0次
// 定位开启状态 true=开启,false=未开启
let bool = false
 
// android平台
if (uni.getSystemInfoSync().platform == 'android') {
  var context = plus.android.importClass("android.content.Context");
  var locationManager = plus.android.importClass("android.location.LocationManager");
  var main = plus.android.runtimeMainActivity();
  var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
  bool = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)
}
 
// ios平台
if (uni.getSystemInfoSync().platform == 'ios') {
  var cllocationManger = plus.ios.import("CLLocationManager");
  var enable = cllocationManger.locationServicesEnabled();
  var status = cllocationManger.authorizationStatus();
  plus.ios.deleteObject(cllocationManger);
  bool = enable && status != 2
}
 
// 未开启定位功能
if (bool === false) {
  uni.showModal({
    title: '提示',
    content: '请打开定位服务',
    success: ({
      confirm,
      cancel
    }) => {
 
      if (confirm) {
        // android平台
        if (uni.getSystemInfoSync().platform == 'android') {
          var Intent = plus.android.importClass('android.content.Intent');
          var Settings = plus.android.importClass('android.provider.Settings');
          var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
          var main = plus.android.runtimeMainActivity();
          main.startActivity(intent); // 打开系统设置GPS服务页面
        }
 
        // ios平台
        if (uni.getSystemInfoSync().platform == 'ios') {
          var UIApplication = plus.ios.import("UIApplication");
          var application2 = UIApplication.sharedApplication();
          var NSURL2 = plus.ios.import("NSURL");
          var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
          application2.openURL(setting2);
          plus.ios.deleteObject(setting2);
          plus.ios.deleteObject(NSURL2);
          plus.ios.deleteObject(application2);
        }
      }
 
      // 用户取消前往开启定位服务
      if (cancel) {
        // do sth...
      }
    }
  });
}

相关文章

网友评论

      本文标题:uniapp提示用户开启定位,跳转到开启定位页面

      本文链接:https://www.haomeiwen.com/subject/sodmvrtx.html