dpa99c / cordova-plugin-request-location-accuracy

Cordova/Phonegap plugin for Android and iOS to request enabling/changing of Location Services by triggering a native dialog
96 stars 62 forks source link

cordova.plugins.locationAccuracy.request() is not opening native settings? #40

Closed abhishekthakur1995 closed 7 years ago

abhishekthakur1995 commented 7 years ago

I have turned off the location and when cordova.plugins.locationAccuracy.request(successCB, errorCB) executes nothing happens and the control goes to its success callback. Native Settings does not open. Working excellent in android though.

dpa99c commented 7 years ago

Please given more details, such as OS version (e.g. iOS 11), platform version (e.g. cordova-ios@4.3.1) and plugin version (e.g. cordova-plugin-request-location-accuracy@2.2.2)

abhishekthakur1995 commented 7 years ago

ios-platform-version : 4.3.1 cordova-plugin-request-location-accuracy@2.2.2 ios version : 10.12.6

dpa99c commented 7 years ago

I've re-tested the scenario you describe using the example project and the plugin appears to behave as expected - see this screencapture.

Please try to build and run the example project yourself and see if you can replicate the issue in order to eliminate the possibility of a bug in your implementation

abhishekthakur1995 commented 7 years ago
`CommonFactory.enableLocation = function(successCallBack) {
        cordova.plugins.locationAccuracy.canRequest(function(canRequest){
            if(canRequest){
                console.log(canRequest);
                cordova.plugins.locationAccuracy.request(function(){
                    successCallBack();
                }, function (error){
                    if(error){
                        if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){
                            cordova.plugins.diagnostic.switchToLocationSettings();
                        }
                    }
                }, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY // iOS will ignore this
                );
            }
        });
    }`

can you check if the code is correct...it would be very helpful.

dpa99c commented 7 years ago

The code looks correct. Check that the app has location permission when this is called. Also note the iOS caveat regarding pressing of the "Cancel" button.

abhishekthakur1995 commented 7 years ago

I am disabling the location on start as you showed in the screencapture .... so i don't think if it would have location permission when the request() is called.

dpa99c commented 7 years ago

This plugin is designed to be used in conjunction with cordova-diagnostic-plugin, so you should do something like:

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

function handleLocationAuthorizationStatus(cb, status){
    switch(status){
       case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
           requestLocationAuthorization(cb);
           break;
       case cordova.plugins.diagnostic.permissionStatus.DENIED:
           cb(false);
           break;
       case cordova.plugins.diagnostic.permissionStatus.GRANTED:
           cb(true);
           break;
       case cordova.plugins.diagnostic.permissionStatus.GRANTED_WHEN_IN_USE:
           cb(true);
           break;
   }
}

function requestLocationAuthorization(cb){
    cordova.plugins.diagnostic.requestLocationAuthorization(handleLocationAuthorizationStatus.bind(this, cb), onError);
}

function ensureLocationAuthorization(cb){
    cordova.plugins.diagnostic.getLocationAuthorizationStatus(handleLocationAuthorizationStatus.bind(this, cb), onError);
}

CommonFactory.enableLocation = function(successCallBack) {
    ensureLocationAuthorization(function(isAuthorized){
        if(isAuthorized){
            cordova.plugins.locationAccuracy.canRequest(function(canRequest){
                if(canRequest){
                    console.log(canRequest);
                    cordova.plugins.locationAccuracy.request(function(){
                        successCallBack();
                    }, function (error){
                        if(error){
                            if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){
                                cordova.plugins.diagnostic.switchToLocationSettings();
                            }
                        }
                    }, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY // iOS will ignore this
                    );
                }
            });
        }else{
            onError("Permission denied");
        }
    });
}