![](/img/trans.png)
[英]IOS 8.0 Swift Request Location Permission requestAlwaysAuthorization is not prompting
[英]Cordova iOS plugin not prompting user for location permission
我正在為我的應用程序編寫一個 iOS 科爾多瓦插件,該插件試圖通過 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 文檔中進行了描述(請參閱此處)
核心位置限制對 requestAlwaysAuthorization() 的調用。 在您的應用調用此方法后,進一步調用將無效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.