其他分享
首页 > 其他分享> > cordova(ios android)位置权限 gps位置开关开启 跳到设置界面

cordova(ios android)位置权限 gps位置开关开启 跳到设置界面

作者:互联网

文章目录

1.cordova插件代码

插件:
cordova-diagnostic-plugin (github)

代码:





//授权都是ok的才会给回调,否则不会有回调,去找用户要授权去了
function locationAvailable(callBack) {
    //位置是否授权
    cordova.plugins.diagnostic.isLocationAuthorized(function (authorized) {
        console.log("Location is " + (authorized ? "authorized" : "unauthorized"));
        if (authorized) {
            isLocationEnabled(callBack)
        } else {
            getLocationAuthorizationStatus()
        }
    }, function (error) {
        console.error("The following error occurred: " + error);
    });
}

function isLocationEnabled(callBack) {
    //gps是否打开,设置里的位置开关是否打开
    cordova.plugins.diagnostic.isLocationEnabled(function (enabled) {
        console.log("Location setting is " + (enabled ? "enabled" : "disabled"));
        if (enabled) {
            callBack()
        } else {
            function onConfirm(buttonIndex) {
                if (buttonIndex == 2) {
                    cordova.plugins.diagnostic.switchToLocationSettings();
                }
            }
            navigator.notification.confirm(
                Language.gpsOpenAlert, // message
                onConfirm,            // callback to invoke with index of button pressed
                Language.prompt,           // title
                [Language.cancel, Language.done]     // buttonLabels
            );
        }
    }, function (error) {
        console.error("The following error occurred: " + error);
    });
}

function getLocationAuthorizationStatus() {
    cordova.plugins.diagnostic.getLocationAuthorizationStatus(function (status) {
        console.log(status)
        switch (status) {
            case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
                console.log("Permission not requested");
                break;
            case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
                console.log("Permission denied always");
                break;
            case cordova.plugins.diagnostic.permissionStatus.GRANTED:
                console.log("Permission granted always");
                break;
            case cordova.plugins.diagnostic.permissionStatus.GRANTED_WHEN_IN_USE:
                console.log("Permission granted only when in use");
                break;
            case cordova.plugins.diagnostic.permissionStatus.DENIED_ONCE:
                console.log("Permission denied once");
                break;
        }
        
        if (status == cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS) {
            //弹框后跳到设置界面
            console.log("弹框")
            function onConfirm(buttonIndex) {
                if (buttonIndex == 2) {
                    console.log("跳到设置界面")
                    cordova.plugins.diagnostic.switchToSettings(function(){
                        console.log("Successfully switched to Settings app");
                    }, function(error){
                        console.error("The following error occurred: "+error);
                    });
                }
            }
            navigator.notification.confirm(
                Language.locationAuthoritied, // message
                onConfirm,            // function(){} callback to invoke with index of button pressed
                Language.prompt,           // title
                [Language.cancel, Language.done]     // buttonLabels
            );
            return
        }
        //请求位置授权 ios,android都可以呼出,并且有用户选择的回调
        cordova.plugins.diagnostic.requestLocationAuthorization(function(status){
            console.log(status);//authorized_when_in_use
        }, function(error){
            console.error(error);
        }, cordova.plugins.diagnostic.locationAuthorizationMode.WHEN_IN_USE);//ALWAYS



    }, function (error) {
        console.error("The following error occurred: " + error);
    });

}

2.iOS请求用户授权的系统位置弹框也是可以用代码调出来的

示例:
在这里插入图片描述
代码:

       //请求位置授权 ios,android都可以呼出,并且有用户选择的回调
        cordova.plugins.diagnostic.requestLocationAuthorization(function(status){
         console.log(status);//authorized_when_in_use
        }, function(error){
            console.error(error);
        }, cordova.plugins.diagnostic.locationAuthorizationMode.WHEN_IN_USE);//ALWAYS

标签:function,console,plugins,ios,diagnostic,error,android,cordova
来源: https://blog.csdn.net/baidu_40537062/article/details/111670679