Location permission alert on iPhone with Cordova

折月煮酒 提交于 2019-11-28 01:40:35
trailing slash

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!