Пытаюсь добавить WeatherKit в мой виджет
Мне нужно внедрить WeatherKit в виджет macOS. Получив текущую погоду на основе вашего текущего местоположения, приложение преобразует currentWeather.condition в одну из четырех строк, где приложение выбирает изображение в зависимости от указанной строки.
Я возился с кучей разных мест размещения и вариантов кода, но ничего не сработало. Ничего не отражается в представлении изображения, и ничего не выходит из консоли, когда я помещаю простую строку печати в блок fetchLocation. Я также подумал, что это может быть проблема с получением текущего местоположения, поэтому я вставил координаты заполнителя, но возникла та же проблема.
Не могли бы вы, ребята, помочь мне найти правильный код и места размещения, чтобы это заработало?
WeatherKit работает в основном приложении, и функция WeatherKit включена в целевом виджете.
Это виджет на данный момент (за исключением ненужного тела):
import WidgetKit
import WeatherKit
import SwiftUI
import CoreLocation
import Intents
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), weatherString: "sunny")
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration, weatherString: "sunny")
completion(entry)
}
var widgetLocationManager = WidgetLocationManager()
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
Task {
var entries: [SimpleEntry] = []
// var widgetLocationManager = WidgetLocationManager()
let service = WeatherService()
// widgetLocationManager.fetchLocation(handler: { location in
do {
var weatherString = "sunny"
let weather = try await service.weather(for: CLLocation(latitude: 40.7128, longitude: -74.0060))
switch weather.currentWeather.condition {
case .clear, .mostlyClear, .partlyCloudy: weatherString = "snowy"
case .cloudy, .blowingDust, .foggy, .haze, .mostlyCloudy, .smoky: weatherString = "cloudy"
case .hot, .frigid:
if weather.currentWeather.cloudCover < 0.5 {
weatherString = "sunny"
} else {
weatherString = "cloudy"
}
case .drizzle, .heavyRain, .isolatedThunderstorms, .rain, .sunShowers, .scatteredThunderstorms, .strongStorms, .thunderstorms, .hail, .hurricane, .tropicalStorm: weatherString = "rainy"
case .flurries, .sleet, .snow, .sunFlurries, .wintryMix, .blizzard, .blowingSnow, .freezingRain, .freezingDrizzle, .heavySnow: weatherString = "snowy"
default: break
}
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate, configuration: configuration, weatherString: weatherString)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
} catch {
assertionFailure(error.localizedDescription)
}
}
// })
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
let weatherString: String
}
struct Clock_WidgetEntryView: View {
var entry: Provider.Entry
var body: some View {
VStack {
Image(entry.weatherString)
.resizable()
.aspectRatio(contentMode: .fit)
}.background(Color.black)
}
}
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)
}
}
@main
struct Clock_Widget: Widget {
let kind: String = "Clock_Widget"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
Clock_WidgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct Clock_Widget_Previews: PreviewProvider {
static var previews: some View {
Clock_WidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), weatherString: "sunny"))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}