11

I use the plugin cordova-plugin-geolocation. My only issue is that the message prompt to allow location looks like this:

/var/container/bundle/application/xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx/my_project/www/index.html Would like to use your location.

Is there anyway to have something a little bit sexier, like

my_project would like to use your location

Cheers.

Added some code, for the non believers

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady(){

    navigator.geolocation.getCurrentPosition(onLocationSuccess, onLocationError, {maximumAge:3000, timeout:2000, enableHighAccuracy:true});

    function onLocationSuccess(){

    }

    function onLocationError(){

    }
 }
Eric
  • 9,870
  • 14
  • 66
  • 102
  • 1
    The geolocation plugin should automatically take care of this for you. Make sure to wait for `deviceready` before your first GPS request -- otherwise you'll get the browser's implementation instead of the plugin's implementation. – Kerri Shotts Nov 26 '16 at 17:56
  • I want for ondevice ready already – Eric Nov 27 '16 at 02:57
  • Would help to see some code. – Kerri Shotts Nov 27 '16 at 22:52
  • Code added for the non believers – Eric Nov 28 '16 at 15:20
  • What version of the plugin are you using? Also, if you log or alert the contents of navigator.geolocation.getCurrentPosition, what do you get? If you see "[native code]" or the like, then something's not allowing the plugin to initialize. – Kerri Shotts Nov 28 '16 at 18:43
  • The plugin works, it's the message that I want to change. cordova-plugin-geolocation 2.4.0 – Eric Nov 28 '16 at 18:51
  • I get that, but the webview has its own native implementation of geolocation. The native code is what generates the alert you're trying to avoid, so I'm trying to rule out some sort of initialization problem with the Cordova plugin (which should override the webview's native implementation). – Kerri Shotts Nov 28 '16 at 19:01
  • Yes I get [native code] – Eric Nov 28 '16 at 19:05
  • Yup uninstalling and reinstalling the plugin fixed it, I don't get the warning nor native code, thanks for your help, closing this question – Eric Nov 29 '16 at 20:08
  • Go through : http://stackoverflow.com/questions/28891339/fix-cordova-geolocation-ask-for-location-message – Hardik Vaghani Dec 05 '16 at 06:51
  • @Bruno Peres has highlighted the answer for your from the plugin Docs. Apache cordova is passing an empty string and you get the generic message – Andy Donegan Dec 05 '16 at 08:58
  • Since the mods have deleted my answer, I will re-add it as a comment. Kerri helped me solved the issue and I tried to closed this bounty but they won't let me. I won't close it/award then. Thanks mods. – Eric Dec 05 '16 at 13:33

5 Answers5

15

The solution has changed for cordova-plugin-geolocation: "4.0.0". This is what you need to add in your config.xml:

<edit-config target="NSLocationWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
    <string>need location access to find things nearby</string>
</edit-config>

For more information: https://github.com/apache/cordova-plugin-geolocation

Stan
  • 1,251
  • 1
  • 15
  • 24
9

From docs:

iOS Quirks

Since iOS 10 it's mandatory to add a NSLocationWhenInUseUsageDescription entry in the info.plist.

NSLocationWhenInUseUsageDescription describes the reason that the app accesses the user's location. When the system prompts the user to allow access, this string is displayed as part of the dialog box. To add this entry you can pass the variable GEOLOCATION_USAGE_DESCRIPTION on plugin install.

Example: cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="your usage message"

If you don't pass the variable, the plugin will add an empty string as value.

To solve your problem, try:

Uninstall the plugin:

cordova plugin remove cordova-plugin-geolocation

Reinstall with:

cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="my_project would like to use your location"
Community
  • 1
  • 1
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • @Eric This is your answer right here. I used the plugin and had not setup the info.plist which resulted in the same issue as yourself. If you do not know how to add the detail into info.plist add another question or search for it. But this is perfect and answers your bounty. – Andy Donegan Dec 05 '16 at 08:56
  • GEOLOCATION_USAGE_DESCRIPTION is for the "privacy - Location Usage Description", what is the variable name for "privacy - Location Always Usage Description" and "privacy - Location When in Use Description"? – h3nr1ke Aug 29 '17 at 01:05
3

There are 3 possible reasons for displaying the path to the index.html instead of the name of your app:

  1. You have not installed the plugin (or it's not installed correctly)
  2. You are not waiting for the device ready event to call the plugin
  3. You have not linked the cordova.js file in the index.html

As you say that you have installed the plugin and you are using it on device ready event, then it must be that you have forgotten to link the cordova.js in the index.html or the plugin wasn't installed correctly. Check that you have the cordova.js linked and if you have it, remove the plugin and add it again, removing and re adding the iOS platform might help too

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
2

It seems like the version 3.0.0 of cordova-plugin-geolocation ignores the install parameter

--variable GEOLOCATION_USAGE_DESCRIPTION=""

like Bruno Peres said above.

It works fine for me installing the 2.4.3 version.

Regolith
  • 2,944
  • 9
  • 33
  • 50
carlo318
  • 35
  • 2
  • 9
1

If you are targetting iOS 8 and later (I my opinion, no need to target older versions anymore...), there are two keys available for configuration:

NSLocationAlwaysUsageDescription

This key lets you describe the reason your app accesses the user’s location information at all times. Include this key when your app uses location services in a potentially nonobvious way while running in the foreground or the background. For example, a social app might include this key when it uses location information to track the user’s location and display other users that are nearby. In this case, the fact that the app is tracking the user’s location might not be readily apparent. The system includes the value of this key in the alert panel displayed to the user when requesting permission to use location services.

NSLocationWhenInUseUsageDescription

This key lets you describe the reason your app accesses the user’s location information while your app runs in the foreground and otherwise when in use. Include this key when your app uses location services to track the user’s current location directly. This key does not support using location services to monitor regions or monitor the user’s location using the significant location change service. The system includes the value of this key in the alert panel displayed to the user when requesting permission to use location services.

So in your iOS project info.plist file you can add the following:

<key>NSLocationAlwaysUsageDescription</key>
<string>my_project requires constant access to your location, even when the screen is off.</string>

Access location only when app is used

<key>NSLocationWhenInUseUsageDescription</key>
<string>my_project requires access to your location only when being used.</string>
Community
  • 1
  • 1
Niko
  • 3,412
  • 26
  • 35
  • I've added the keys and strings in a exportOptions.plist (only plist file I could find) in platform/iOS, and it didn't change anything... – Eric Nov 28 '16 at 19:02