Ionic android ask for location permission

10,079

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.

Share:
10,079
MagicDragon
Author by

MagicDragon

Updated on August 03, 2022

Comments

  • MagicDragon
    MagicDragon over 1 year

    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" />
    • cordova-plugin-android-premissions

    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:

    • Cordova CLI: 6.2.0
    • Gulp CLI & local: 3.9.1
    • Ionic Framework: 1.3.1
    • Ionic CLI: 1.7.16
    • Node: 4.2.2
    • Device android: 5.0.1
    • Desired build target: 16
    • Current build target: 22
  • MagicDragon
    MagicDragon over 7 years
    Everything is working fine now! Thanks a lot @DaveAlden for your great support and for summarizing the possibilities!
  • Łukasz Ksawery Rozbicki
    Łukasz Ksawery Rozbicki over 7 years
    Hi @DaveAlden! Maybe you'll help me! I have an app using geolocation. Everything worked fine until new version release. The new version contains manifest change that Google says we should do (uses-feature with hardware.location.gps). And now my app cannot get current position :/ I have those in my manifest file: ACCESS_LOCATION_EXTRA_COMMANDS ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION android.hardware.location.gps android.hardware.location.network
  • Łukasz Ksawery Rozbicki
    Łukasz Ksawery Rozbicki over 7 years
    When I'm using your diagnostic plugin everything seems to be ok. Those methods return true or info about high accuracy: getLocationAuthorizationStatus isNetworkLocationEnabled isNetworkLocationAvailable isGpsLocationEnabled isGpsLocationAvailable getLocationMode requestLocationAuthorization isLocationEnabled isLocationAuthorized isLocationAvailable Still, when I'm trying to get position the result is "position denied". This problem is on Android 4.3, 4.4, 5.0 and 5.1. Sometimes on 6.x too. My app are able to get position on some devices but on others it fails.