Search code examples
ibm-mobilefirst

Using getCurrentPosition on Android 6.x


Version: 7.1.0.00-20161006-0540 OS:Android 6.0.1

When trying to use the getCurrentPosition function, it fails with Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions.

But does not show the dialog to allow the permission. Im assuming that the plugin would handle this for you. If not is there a way with JS to show the user the prompt?


Solution

  • Ensure you have the following in your application, per the documentation:
    https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/7.1/advanced-client-side-development/location-services-hybrid-applications/#androidPermissions

    Android Marshmallow requires user permissions to be granted at runtime. Since runtime code written in Javascript does not have access to the Android api, these permissions must be requested and checked by the native code before launching the Cordova Web Framework. The file [application name].java in the native folder is responsible for loading the web resources. The code for requesting and checking permissions for Location Services should be called within this file, for example in onInitWebFrameworkComplete.

    public void onInitWebFrameworkComplete(WLInitWebFrameworkResult result) {
        if (result.getStatusCode() == WLInitWebFrameworkResult.SUCCESS) {
            super.loadUrl(WL.getInstance().getMainHtmlFilePath());
        } else {
            handleWebFrameworkInitFailure(result);
        }
    
        if (! (WLClient.getInstance().getContext().checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[ {
                android.Manifest.permission.ACCESS_FINE_LOCATION}, 0);
        }
    }