xcodewidgetkit

Xcode 15 WidgetKit: My Widget Is Completely Black On A Real Device


My app supports a widget. Everything works fine in the simulator running IOS 17, however, when I tested it on a real device, my widget turns completely black.

I used the same devices in both cases, which is why I find it so weird. How come the same devices produce different results?

Here's a snippet of my code that fixed my "black screen" issue on IOS 17 Simulator:

struct myWidget: Widget {
    let kind: String = "myWidget"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            let color = myBgColor
            // this fixed my IOS 17 Simulator "black screen" issue
            if #available(iOS 17.0, *) {
                myWidgetEntryView(entry: entry)
                    .containerBackground(.linearGradient(AnyGradient(Gradient(colors:  [color, color.lighten(percent: 5) , color.lighten(percent: 15)])), startPoint: .top, endPoint: .bottom), for: .widget)
            } else {
                myWidgetEntryView(entry: entry)
                    .padding()
                    .background(LinearGradient(colors: [color, color.lighten(percent: 5) , color.lighten(percent: 15)], startPoint: .top, endPoint: .bottom))
            }
        }
        .configurationDisplayName("Title")
        .description("Description")
        .supportedFamilies([.systemSmall])
    }
}

The rest of the widget shouldn't affect the result, as I only return a Text() for now in order to isolate the problem.

Anyone experiencing a similar problem?


Solution

  • With iOS 17 I have faced issue with widget, idk if it will solve yours but hopefully it will!

    So I just fixed them with two things. First I used:

    .containerBackground() for iOS 17.

    Then, I used AppStorage instead of Userdefault to move data from my App to Widget. I used this tutorial: https://www.youtube.com/watch?v=Z_m5d8RS4xU

    At 27:00 he started to implement the container and uses the AppStorage.

    Thanks!