在使用swiftui的小部件中,如何获取当前位置?
如何在小部件中获取用户当前位置?我用的是swiftUI在使用swiftui的小部件中,如何获取当前位置?,swiftui,widget,core-location,Swiftui,Widget,Core Location,如何在小部件中获取用户当前位置?我用的是swiftUI class WidgetLocationManager: NSObject, CLLocationManagerDelegate { var locationManager: CLLocationManager? private var handler: ((CLLocation) -> Void)? override init() { super.init() Dispat
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(对于配置:SelectPlaceIntent,在context:context中,完成:@escaping(Timeline)->Void){
widgetLocationManager.fetchLocation(处理程序:{中的位置
打印(位置)
.......
})
}
var widgetLocationManager = WidgetLocationManager()
func getTimeline(for configuration: SelectPlaceIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
widgetLocationManager.fetchLocation(handler: { location in
print(location)
.......
})
}