Swiftui CoreLocation WidgetKit
在Swiftui CoreLocation WidgetKit,swiftui,core-location,widgetkit,Swiftui,Core Location,Widgetkit,在WidgetKit工作时,我在获取CoreLocation时遇到问题。我可以在SwiftUI应用程序中使用observateObject类来实现这一点。现在我不知道如何使用小部件来实现这一点 我将LocationManager添加到我的TimelineEntry中,然后调用getTimeline()中的requestLocation函数,但它也不起作用。我在我的小部件中没有看到任何更新。如前所述,我将正确的键添加到我的小部件中 导入基础 导入核心定位 导入WidgetKit 类Location
WidgetKit
工作时,我在获取CoreLocation
时遇到问题。我可以在SwiftUI应用程序中使用observateObject
类来实现这一点。现在我不知道如何使用小部件来实现这一点
我将LocationManager
添加到我的TimelineEntry
中,然后调用getTimeline()
中的requestLocation
函数,但它也不起作用。我在我的小部件中没有看到任何更新。如前所述,我将正确的键添加到我的小部件中
<代码>导入基础
导入核心定位
导入WidgetKit
类LocationManager:NSObject,CLLocationManagerDelegate{
var locationManager=CLLocationManager()
变量位置:CLLocation?
重写init(){
super.init()
locationManager.delegate=self
locationManager.desiredAccuracy=KCallocationAccuracyBest
locationManager.RequestWhenUseAuthorization()
locationManager.startUpdatingLocation()
WidgetCenter.shared.reloadAllTimelines()
}
func requestUpdate(u完成:@escaping()->Void){
self.locationManager.requestWhenUseAuthorization()
self.locationManager.startUpdatingLocation()
WidgetCenter.shared.reloadAllTimelines()
}
func locationManager(manager:CLLocationManager,didUpdateLocations位置:[CLLocation]){
guard let location=locations.last else{return}
self.location=位置
}
func locationManager(manager:CLLocationManager,didFailWithError:error){
打印(错误。本地化描述)
}
}
将我的位置管理器移动到
TimelineProvider
,并将我的变量添加到我的小部件TimelineEntry
<代码>核心位置现在正在正确更新