Setting NSLocationWhenInUseUsageDescription value

2019-01-28 10:02发布

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.

3条回答
女痞
2楼-- · 2019-01-28 10:44

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>
查看更多
甜甜的少女心
3楼-- · 2019-01-28 10:46

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
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-28 10:52

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"
查看更多
登录 后发表回答