使用cordova-plugin-geolocation设置NSLocationWhenInUseUsageDescription值 [英] Setting NSLocationWhenInUseUsageDescription value when using cordova-plugin-geolocation
问题描述
我正在使用Cordova构建移动应用程序.我使用cordova-plugin-geolocation
来获取用户的当前位置.要在iOS上自定义位置权限请求,需要设置NSLocationWhenInUseUsageDescription
plist值.
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.
通常,要设置iOS plist选项,需要在platforms/ios/<app-name>/<app-name>-Info.plist
中手动设置选项值.问题在于,在每个版本上,cordova-plugin-geolocation
都会使用默认的空字符串覆盖自定义值.
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.
猜测与plugins/cordova-plugin-geolocation/plugin.xml
中的以下几行有关:
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>
但是,如果我尝试在该<string>
中设置任何值,则该值将不受尊重,因为安装插件后,plugin.xml
似乎已缓存在某个地方.
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.
因此,对于在项目重建过程中保持持久性设置NSLocationWhenInUseUsageDescription
的任何帮助,我们深表感谢.
So any help with setting the NSLocationWhenInUseUsageDescription
in a way that persists over project rebuilds is appreciated.
推荐答案
新答案:
如果使用Cordova CLI 6.5.0或更高版本的cordova-plugin-geolocation
(> = 3.0.0),则应在config.xml
中使用edit-config
标记,如下所示:
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>
过时的旧答案: 分叉github插件
OLD OUTDATED ANSWER: Fork the github plugin
用所需的文本更改NSLocationWhenInUseUsageDescription
<config-file target="*-Info.plist" parent="NSLocationWhenInUseUsageDescription">
<string>your custom text here</string>
</config-file>
然后删除原始的地理位置插件,并使用来安装您的fork
then remove the original geolocation plugin and install your fork with
cordova plugin add https://github.com/yourUsername/yourGeolocationFork.git
这篇关于使用cordova-plugin-geolocation设置NSLocationWhenInUseUsageDescription值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!