3

I'm working on a cordova app on which I have to locate the user latitude and longitude. Using the geolocation plugin, it works fine on android devices but it display an alert asking for permission from user in iOS. When I used the simulator I get this alert message:

Users/user/Library/Developer/CoreSimulator/Devices/783A2EFD-2976-448C-8E4E-841C985D337D/data/Containers/Bundle/Application/EFC846BB-4BA3-465C-BD44-575582E649FC/app_name.app/www/index.html would like to use your current location.

I have seen topic talking about this problem like: this and thisbut none of the provided solutions works for me.

this is the cordova example page:

<!DOCTYPE html>
 <html>
<head>
<title>Device Properties Example</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +
                        'Longitude: '          + position.coords.longitude             + '<br />' +
                        'Altitude: '           + position.coords.altitude              + '<br />' +
                        'Accuracy: '           + position.coords.accuracy              + '<br />' +
                        'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +
                        'Heading: '            + position.coords.heading               + '<br />' +
                        'Speed: '              + position.coords.speed                 + '<br />' +
                        'Timestamp: '          + position.timestamp                    + '<br />';
}

function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

</script>
</head>
<body>
<p id="geolocation">Finding geolocation...</p>
</body>

Is there any way to change the text of the alert or to disable this alert?

-edit---

I have found the source of my problem. I removed the geolocation plugin and add it several times because when I have added the plugin I haven't found a folder with the name of the geolocation plugin like the other plugins. Even the cordova_plugin.js file doesn't contain any data about geolocation plugin. Now I have installed the plugin again and it works.

Community
  • 1
  • 1
Dev DOS
  • 1,018
  • 4
  • 18
  • 45
  • 1
    Do not call getCurrentPosition immediately after deviceready has been fired. Did you give setTimeout a try to call getCurrentPosition after some delay? – Blauharley Mar 27 '15 at 18:48
  • Yes even with setTimeout(function(){ navigator.geolocation.getCurrentPosition(onSuccess, onError); }, 60000); the ugly HTML message is displaying. – Dev DOS Mar 30 '15 at 08:11
  • @Dev DOS how you remove this error – nivritgupta Sep 21 '15 at 12:29
  • @nivritgupa I don't remember how exactly but I remember that It was a problem related to the geolocation plugin so :remove/add the geolocation plugin: `cordova plugin rm org.apache.cordova.geolocation cordova plugin add org.apache.cordova.geolocation` add NSLocationWhenInUseUsageDescription to the .plist. Open /platforms/ios/{project}/{project}-Info.plist and add the following: `NSLocationWhenInUseUsageDescription [App Name] would like to access your location when running and displayed.` – Dev DOS Sep 21 '15 at 19:23

3 Answers3

5

Same as the "edit" in the original question, I had to remove the old version of the geolocation plugin and add the new one. Then I had to remove/add the Cordova iOS platform. Only then could I add NSLocationWhenInUseUsageDescription to the .plist file as DaveAlden mentions in his answer with success.

First, remove/add the geolocation plugin:

cordova plugin rm org.apache.cordova.geolocation
cordova plugin add org.apache.cordova.geolocation

Second, remove/add the iOS platform:

cordova platform rm ios
cordova platform add ios

Last, add NSLocationWhenInUseUsageDescription to the .plist. Open /platforms/ios/{project}/{project}-Info.plist and add the following:

<key>NSLocationWhenInUseUsageDescription</key>
<string>[App Name] would like to access your location when running and displayed.</string>

See this iOS Developer Library link for detailed information regarding NSLocationWhenInUseUsageDescription versus NSLocationAlwaysUsageDescription versus NSLocationUsageDescription.

Community
  • 1
  • 1
trailing slash
  • 881
  • 11
  • 13
  • Hello, How to Disable Location permission alert on iPhone with Cordova. – KAUSHAL J. SATHWARA Oct 26 '15 at 10:45
  • 2
    I don't believe it's possible to disable the alert. It's my understanding that it's hardwired into the OS as a basic security feature to only permit transferring this type of private information (device location) to a 3rd party application with explicit user allowance. – trailing slash Oct 28 '15 at 17:16
2
  • Make sure "cordova.js" file exists in your root folder and just by including this JS file in your .html file where you are accessing this location will resolve this issue. NOTHING FANCY REQUIRED. If this js file is missing, the navigator.geolocation will call the HTML5 object which is browser based and hence the weird alert.
mahipalz
  • 31
  • 2
1

You can't disable the request message, either in the emulator or on the device, but you can customise it by adding a property to your project's .plist.

For iOS 8, if your app requests permission to use location in the background, you want the following key (set the string value to anything you like):

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

If your app only uses location while in the foreground, then add the following key:

<key>NSLocationWhenInUseUsageDescription</key>
<string>My app requires access to your location when the screen is on and the app is displayed.</string>

For older versions of iOS (<=7) you need to use NSLocationUsageDescription

DaveAlden
  • 30,083
  • 11
  • 93
  • 155
  • do you mean config.xml file? because I can't find the .plist file in the project – Dev DOS Mar 29 '15 at 14:50
  • If you are building your cordova app locally, your [plist file](https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html) will be `/{project}/platforms/ios/{project}/{project}-Info.plist` – DaveAlden Mar 29 '15 at 19:55
  • I found that my app use NSLocationWhenInUseUsageDescription with no string value when I have changed it with my custom text it changes the first message when I run my app for the first time, but the second ugly message is still displaying. How can I change it? – Dev DOS Mar 30 '15 at 08:03