Issue with fetching WeatherKit data on widget

203 views Asked by At

I am trying to get the user's current weather condition using WeatherKit on the widget, the main app is working fine. But the widget app is not working at all, I mean it doesn't fetch any data from weather service. I have enabled weatherkit on both main and widget app ID, and also added WeatherKit capabllity for both targets.

When I try to fetch data nothing is happening, even the widget's debugger doesn't give me any logs!

Here is my code:

 func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        Task {
            await viewModel.getCurrentWeather() //fetching data
            
            var entries: [SimpleEntry] = []
            // Generate a timeline consisting of five entries an hour apart, starting from the current date.
            let currentDate = Date()
            for hourOffset in 0 ..< 5 {
                print(viewModel.locationManager.locationName)
                let entryDate = Calendar.current.date(byAdding: .minute, value: hourOffset, to: currentDate)!
                let entry = SimpleEntry(date: entryDate, weather: viewModel.currentWeather)
                entries.append(entry)
            }
            let timeline = Timeline(entries: entries, policy: .atEnd)
            completion(timeline)
            print(completion)
        }
    }
}

struct SimpleEntry: TimelineEntry {
    let date: Date
    let weather: Weather
}

struct Forecast_WidgetsEntryView : View {
    var entry: Provider.Entry
    @Environment(\.widgetFamily) var type
    var body: some View {
        DailyForecast1(weather: entry.weather)
    }
}

I also enabled Widget Wants Location in the widget info.plist to get user current location.

Here is WeatherViewModel class codes:

@MainActor class WeatherViewModel: ObservableObject {
    
    let locationManager = LocationManager()
    @Published var currentWeather: Weather = .empty()
    @Published var isLoading = true
    
    init() { }
    
    func getCurrentWeather() async {
        print("fetching data...")
        isLoading = true
        do {
            guard let currentLocation = locationManager.lastLocation else { return }
            let weather = try await WeatherService.shared.weather(for: currentLocation)
            self.currentWeather = Weather(temperature: weather.currentWeather.temperature.value,
                                          condition: weather.currentWeather.condition.description.capitalized,
                                          symbolName: weather.currentWeather.symbolName,
                                          hightTemp: weather.dailyForecast.first?.highTemperature.value ?? 0.0,
                                          lowTemp: weather.dailyForecast.first?.lowTemperature.value ?? 0.0,
                                          dailyWeathers: weather.dailyForecast.forecast,
                                          hourlyWeathers: weather.hourlyForecast.forecast,
                                          daylight: weather.currentWeather.isDaylight)
            isLoading = false
        } catch {
            isLoading = false
            print(error.localizedDescription)
        }
    }
}
0

There are 0 answers