【发布时间】: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