本文介绍了混合式Cordova应用回调,用于位置权限警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 cordova混合应用程序,该应用程序使用了location权限.因此JavaScript中仍然可以检测在allowcancel警报弹出窗口上执行的操作.像Android中一样,我们有回调onRequestPermissionsResult.

I have a cordova hybrid app which using location permission. so is there anyway in JavaScript to detect the action performed on alert popup either allow or cancel.something like in Android we have callback onRequestPermissionsResult.

我正在使用Geolocation API获取位置navigator.geolocation

navigator.geolocation.getCurrentPosition((position)=> {
    console.log(position);
});

推荐答案

您可以使用 cordova-diagnostic-plugin 要求位置许可:

You can do so by using cordova-diagnostic-plugin to request location permission:

配置它:

config.xml:<preference name="cordova.plugins.diagnostic.modules" value="LOCATION" />

安装:

$ cordova plugin add cordova.plugins.diagnostic

使用 requestLocationAuthorization() :

cordova.plugins.diagnostic.requestLocationAuthorization(function(status){
    switch(status){
        case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
            console.log("Permission not requested");
            break;
        case cordova.plugins.diagnostic.permissionStatus.GRANTED:
            console.log("Permission granted");
            break;
        case cordova.plugins.diagnostic.permissionStatus.DENIED:
            console.log("Permission denied");
            break;
        case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
            console.log("Permission permanently denied");
            break;
    }
}, function(error){
    console.error(error);
});

这篇关于混合式Cordova应用回调,用于位置权限警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 12:04