简体   繁体   中英

Location permission alert on iPhone with Cordova

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 this but 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.

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 .

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

  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM