gpt4 book ai didi

ios - 在 iOS 14 WidgetKit 中获取当前位置

转载 作者:行者123 更新时间:2023-12-03 09:22:55 25 4
gpt4 key购买 nike

有没有人尝试在 iOS 14 WidgetKit 中更新用户的位置?
在阅读了 Apple 开发者论坛后,我提出了关于 CLLocationManager 的写作包装。并以这种方式使用它:

class WidgetLocationManager: NSObject, CLLocationManagerDelegate {
var locationManager: CLLocationManager? {
didSet {
self.locationManager!.delegate = self
}
}
private var handler: ((CLLocation) -> Void)?

func fetchLocation(handler: @escaping (CLLocation) -> Void) {
self.handler = handler
self.locationManager!.requestLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.handler!(locations.last!)
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}
并以这种方式使用它:
var widgetLocationManager = WidgetLocationManager()
func getTimeline(for configuration: SelectPlaceIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
if widgetLocationManager.locationManager == nil {
widgetLocationManager.locationManager = CLLocationManager()
widgetLocationManager.locationManager!.requestWhenInUseAuthorization()
}
widgetLocationManager.fetchLocation(handler: { location in
print(location)
.......
})
}
我在 Widget 的 info.plist 中也有这两个条目:
<key>NSLocationUsageDescription</key>
<string>1</string>

<key>NSWidgetWantsLocation</key>
<true/>
当 locationManager.requestLocation() 被调用时,授权状态为 authorisedWhenInUse,但委托(delegate)的方法永远不会被调用。我错过了什么?

最佳答案

首先,我看到的明显问题:

<key>NSLocationUsageDescription</key>
<string>1</string>
NSLocationUsageDescription已弃用: Apple Documentation , 所以你应该使用 NSLocationWhenInUseUsageDescriptionNSLocationAlwaysAndWhenInUseUsageDescription反而。请务必在主应用程序中包含您选择的权限 Info.plist以及
此外,创建 CLLocationManager
func getTimeline(for configuration: SelectPlaceIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
...
}
可能有问题,因为它可以从后台线程调用,所以我会重构你的 WidgetLocationManager像这样:
class WidgetLocationManager: NSObject, CLLocationManagerDelegate {
var locationManager: CLLocationManager?
private var handler: ((CLLocation) -> Void)?

override init() {
super.init()
DispatchQueue.main.async {
self.locationManager = CLLocationManager()
self.locationManager!.delegate = self
if self.locationManager!.authorizationStatus == .notDetermined {
self.locationManager!.requestWhenInUseAuthorization()
}
}
}

func fetchLocation(handler: @escaping (CLLocation) -> Void) {
self.handler = handler
self.locationManager!.requestLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.handler!(locations.last!)
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}
然后像这样使用它:
var widgetLocationManager = WidgetLocationManager()

func getTimeline(for configuration: SelectPlaceIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
widgetLocationManager.fetchLocation(handler: { location in
print(location)
.......
})
}

关于ios - 在 iOS 14 WidgetKit 中获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63844536/

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