【问题标题】:Setting NSLocationWhenInUseUsageDescription value when using cordova-plugin-geolocation使用 cordova-plugin-geolocation 时设置 NSLocationWhenInUseUsageDescription 值
【发布时间】:2015-11-10 19:05:03
【问题描述】:

我正在使用 Cordova 构建一个移动应用程序。我使用cordova-plugin-geolocation 来获取用户的当前位置。要在 iOS 上自定义位置权限请求,需要设置 NSLocationWhenInUseUsageDescription plist 值。

通常,要设置 iOS plist 选项,需要手动设置 platforms/ios/<app-name>/<app-name>-Info.plist 中的选项值。问题在于,在每次构建时,cordova-plugin-geolocation 都会使用默认的空字符串覆盖自定义值。

猜测它与plugins/cordova-plugin-geolocation/plugin.xml 中的以下几行有关:

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

但是,如果我尝试在 &lt;string&gt; 中设置任何值,则不会受到尊重,因为在安装插件时,plugin.xml 似乎被缓存在某处。

因此,对于以在项目重建过程中持续存在的方式设置 NSLocationWhenInUseUsageDescription 的任何帮助,我们将不胜感激。

【问题讨论】:

    标签: cordova cordova-plugins


    【解决方案1】:

    新答案:

    如果使用 Cordova CLI 6.5.0 或更新和最新版本的 cordova-plugin-geolocation (>=3.0.0),您应该在 config.xml 中使用 edit-config 标记,如下所示:

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

    过时的答案: fork github 插件

    用你想要的文字改变NSLocationWhenInUseUsageDescription

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

    然后删除原来的地理定位插件并安装你的fork

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

    【讨论】:

    • 谢谢,我也是这么想的。不好,但可以。
    • phonegap app怎么办?
    • @Jobayer 我已经用推荐的方式更新了我的答案。也适用于Phonegap
    • 在config.xml中添加后要插入吗?
    • @Jobayer 不,在 config.xml 的根目录或 iOS 平台标签内的任何位置,但在插件外部,因为即使您没有安装插件,它也会设置值
    【解决方案2】:

    尚未发布(到 npm)的插件版本支持此功能。

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

    你可以这样安装:

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

    【讨论】:

      【解决方案3】:

      接受的答案不适用于 Phonegap 构建。以下对我有用:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-03
        • 2016-05-24
        • 2018-10-29
        • 2016-12-12
        • 2021-02-06
        • 2020-12-15
        • 2023-03-10
        相关资源
        最近更新 更多