Setting NSLocationWhenInUseUsageDescription value when using cordova-plugin-geolocation

一世执手 提交于 2019-11-28 07:59:29

问题


I'm building a mobile app using Cordova. I use cordova-plugin-geolocation to get the user's current location. To customize the location permission request on iOS it's needed to set the NSLocationWhenInUseUsageDescription plist value.

Generally, to set an iOS plist option it's needed to manually set the option value in the platforms/ios/<app-name>/<app-name>-Info.plist. The problem is that on each build the cordova-plugin-geolocation overwrites the custom value with it's default empty string.

A guess it has something to do with the following lines in the plugins/cordova-plugin-geolocation/plugin.xml:

<config-file target="*-Info.plist" parent="NSLocationWhenInUseUsageDescription">
  <string></string>
</config-file>

But if I try to set any value into that <string> it is not respected, because it seems that the plugin.xml is cached somewhere when the plugin is installed.

So any help with setting the NSLocationWhenInUseUsageDescription in a way that persists over project rebuilds is appreciated.


回答1:


NEW ANSWER:

If using Cordova CLI 6.5.0 or newer and latest version of cordova-plugin-geolocation (>=3.0.0), you should use edit-config tag in the config.xml like this:

<edit-config target="NSLocationWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
    <string>your custom text here</string>
</edit-config>

OLD OUTDATED ANSWER: Fork the github plugin

change the NSLocationWhenInUseUsageDescription with the text you want

<config-file target="*-Info.plist" parent="NSLocationWhenInUseUsageDescription">
    <string>your custom text here</string>
</config-file>

then remove the original geolocation plugin and install your fork with

cordova plugin add https://github.com/yourUsername/yourGeolocationFork.git



回答2:


A not yet released (to npm) version of the plugin supports this.

https://github.com/apache/cordova-plugin-geolocation/commit/f9f7a23c57daad5514ce3d830a2defeb93ba3f78

You can install it like this:

cordova plugin add https://github.com/apache/cordova-plugin-geolocation#f9f7a23c57daad5514ce3d830a2defeb93ba3f78 --save --variable GEOLOCATION_USAGE_DESCRIPTION="Your text here"



回答3:


The accepted answer is not working for me with Phonegap build. The following works for me :

<gap:config-file overwrite="true" parent="NSLocationWhenInUseUsageDescription" platform="ios">
    <string> your custom text here</string>
</gap:config-file>


来源:https://stackoverflow.com/questions/33637591/setting-nslocationwheninuseusagedescription-value-when-using-cordova-plugin-geol

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