gpt4 book ai didi

ios - Cordova iOS 插件不提示用户位置权限

转载 作者:行者123 更新时间:2023-12-05 07:39:51 24 4
gpt4 key购买 nike

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

最佳答案

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

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

它在 Apple 文档中有描述(引用 here )

Core Location limits calls to requestAlwaysAuthorization(). After your app calls this method, further calls have no effect.

关于ios - Cordova iOS 插件不提示用户位置权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46801961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com