What I'd like to accomplish:
If the user's location service is preventing me from getting his/her phone's location by default I'd like to ask for location permission on every API between 16-25 (Android 4.1.2-7.1.1)
Since on iOS I haven't met this problem I'd like to focus on android.
What I've already tried:
<uses-permission android:name="android.premission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.premission.ACCESS_FINE_LOCATION" />
The plugin mentioned above always returns with hasPermission: true
even if the access to location is denied on the device so it's not working for me.
What I haven't tried yet:
Diagnostic plugin description says that I have choose between API 23 and above or API 22 and below in my understanding. (<preference name="android-targetSdkVersion" value="22"/>
is the minimum target version.) Yet I don't know if it would still work on lower API versions.
My setup:
Diagnostic plugin description says that I have choose between API 23 and above or API 22 and below in my understanding. ( is the minimum target version.) Yet I don't know if it would still work on lower API versions.
cordova-diagnostic-plugin API level requirements are based around the SDK version being used to build, not the API version running on the target device. So an app built with SDK for API 23 (Android 6.0) will still work on devices running older versions of Android.
You need to set your build target to API 23 (or above) to build with the primary version of the plugin (cordova.plugins.diagnostic
as opposed to cordova.plugins.diagnostic.api-22
) because it includes the run-time permissions code which requires a build environment of API 23+.
If the user's location service is preventing me from getting his/her phone's location by default I'd like to ask for location permission on every API between 16-25 (Android 4.1.2-7.1.1)
There's two different aspects to this:
ask for location permission on every API between 16-25 (Android 4.1.2-7.1.1)
Android run-time permissions were introduced in API 23 (Android 6.0)
You can check if the app has location authorization using isLocationAuthorized(). Calling it on a device running Android 5 (API 22) or below will always return TRUE as the permissions are already granted at installation time.
You can request runtime permission to use location with requestLocationAuthorization(). Calling it on a device running Android 5 (API 22) or below will have no effect as the permissions are already granted at installation time.
location service is preventing me from getting his/her phone's location
You can check if user has switched off location using isLocationEnabled().
If location is switched off, you can use switchToLocationSettings() to open the device location settings page to allow user to enable location services, or you can use the cordova-plugin-request-location-accuracy to request the desired location accuracy without needing the user to manually do this on the location settings page.
Disclaimer: I am the author of cordova-diagnostic-plugin and cordova-plugin-request-location-accuracy.