【问题标题】:Cordova iOS plugin not prompting user for location permissionCordova iOS 插件不提示用户位置权限
【发布时间】:2018-03-29 20:13:29
【问题描述】:

我正在为我的应用程序编写一个 iOS cordova 插件,该插件试图通过 iOS 的重大更改 API 接收后台地理位置更新。我有以下插件代码:

import os.log;
import CoreLocation;

@objc(MyPlugin) class MyPlugin: CDVPlugin, CLLocationManagerDelegate {
    let locationManager = CLLocationManager()

    override func pluginInitialize() {
        super.pluginInitialize()
        os_log("[MyPlugin] - plugin initialize")

        locationManager.delegate = self;
    }

    func initBackgroundGeolocation(_ command: CDVInvokedUrlCommand) {
        os_log("[MyPlugin] - initBackgroundGeolocation")

        if (CLLocationManager.significantLocationChangeMonitoringAvailable()) {
            os_log("[MyPlugin] - significant location change is available")
            locationManager.requestAlwaysAuthorization();
        } else {
            os_log("[MyPlugin] - significant location change is not available")
        }

        os_log("[MyPlugin] - location manager is configured")

        let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
        self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
    }

    func locationManager(_ manager: CLLocationManager,  didUpdateLocations locations: [CLLocation]) {
        os_log("[MyPlugin] - received a location update")
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {   
        switch status {
        case .restricted, .denied:
            os_log("[MyPlugin] - denied authorization")
            break

        case .authorizedWhenInUse:
            os_log("[MyPlugin] - received when in use authorization")
            break

        case .authorizedAlways:
            os_log("[MyPlugin] - received always usage authorization")
            os_log("[MyPlugin] - starting significant location change monitoring")

            locationManager.startMonitoringSignificantLocationChanges();
            break

        case .notDetermined:
            os_log("[MyPlugin] - status not determined")
            break
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        os_log("[MyPlugin] - did fail with error was called")
    }
}

我相信我通过我的 plugin.xml 正确添加了必要的 Info.plist 条目。以下是相关部分:

    <config-file target="*-Info.plist" parent="NSLocationAlwaysAndWhenInUseUsageDescription">
        <string>$ALWAYS_USAGE_DESCRIPTION</string>
    </config-file>
    <config-file target="*-Info.plist" parent="NSLocationAlwaysUsageDescription">
        <string>$ALWAYS_USAGE_DESCRIPTION</string>
    </config-file>
    <config-file target="*-Info.plist" parent="NSLocationWhenInUseUsageDescription">
        <string>$WHEN_IN_USE_USAGE_DESCRIPTION</string>
    </config-file>

    <config-file target="*-Info.plist" parent="UIBackgroundModes">
        <array>
            <string>location</string>
        </array>
    </config-file>

当我致电locationManager.requestAlwaysAuthorization(); 时,我希望 iOS 会提示用户授予对其位置的访问权限。然而,这并没有发生。我在调试器中单步执行了代码,调用似乎成功执行,但没有任何反应。

我是 iOS、Swift 和 cordova 插件开发的新手,所以我很可能遗漏了一些很明显的东西。非常感谢所有建议!

【问题讨论】:

    标签: ios cordova cordova-plugins cllocationmanager


    【解决方案1】:

    我知道我很晚了,但在这里发布我的答案,因为它可能对某人有帮助。 :)

    requestAlwaysAuthorization 将仅在第一次显示提示。从那时起,不再显示任何提示。

    Apple 文档中有描述(参考here

    核心位置限制对 requestAlwaysAuthorization() 的调用。在您的应用调用此方法后,进一步调用将无效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-30
      • 2016-07-26
      • 2016-06-22
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多