Sunday, November 27, 2022
HomeIOS Developmentios - Strive Information(contentsOf:) throws from iPhone widget when iPhone is locked...

ios – Strive Information(contentsOf:) throws from iPhone widget when iPhone is locked however in any other case works

My widget correctly reads (utilizing a name to Information) from an App Group when the telephone is energetic, however throws when the telephone is locked/inactive.

Particulars. A Climate Occasion is a 336-byte struct written to and skim from a JSON file saved in an App Group. The JSON file is shared between an iPhone app and an iPhone widget.

The iPhone app creates the occasion. The app writes the occasion to the App Group from the place the iPhone widget reads and shows the occasion’s information. The widget may also replace (write to) the climate occasion. This happens after checking–a getTimeline(…) entry–for an up to date climate forecast. The widget updates AS LONG AS the telephone stays energetic, whether or not or not the app is working. All good!

Nevertheless, when the display screen locked, the widget fails to learn the info containing the JSON info. The error happens when attempting to entry the info (which happens earlier than any tried forecasts). The next code simplifies my code whereas additionally failing as described.

func getTimeline(in context: Context, completion: @escaping (Timeline<WeatherEntry>) -> ()) {

    Activity {
        var widgetEvent: Occasion = Occasion()
        do {
            // The "attempt Information" name under throws solely when the iPhone is locked
            let information = attempt Information(contentsOf: FileManager.appEventShare) // <- Throws!
            let widgetEvents: [Event] = attempt JSONDecoder().decode([Event].self, from: information)

            if !widgetEvents.isEmpty {
                widgetEvent = widgetEvents[0]
            else {
                widgetEvent.userData.identify = "No Occasion"
        } catch {
            print("Widget couldn't open occasion", error.localizedDescription)
            widgetEvent.userData.identify = "Failed Decode"
        let currentDate =
        let timelineTime = timeForFiveMinuteInterval(from: currentDate)
        let entries = [
            WeatherEntry(date: currentDate, event: widgetEvent),
            WeatherEntry(date: timelineTime, event: widgetEvent)
        let timeline = Timeline(entries: entries, coverage: .atEnd)

Ideas on what’s taking place?



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments