Cordova iOS插件未提示用户提供位置许可

时间:2017-10-18 02:36:02

标签: ios cordova cordova-plugins cllocationmanager

我正在为我的应用编写一个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插件开发的新手,所以我很可能错过了一些非常明显的东西。非常感谢所有建议!

1 个答案:

答案 0 :(得分:0)

我知道我来晚了,但是在这里发布我的答案可能会对某人有所帮助。 :)

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

它在Apple文档中有所描述(请参阅here

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