1

I am trying to add lock screen widget in existing app which is having .medium widget.

The problem is as soon as I add lock screen widget on device, home screen widget getting blank. Once I remove lock screen widget from device the home screen widget showing correctly.

Here is my code for the view

struct WidgetNewEntryView : View {
    var myDataEntry: DataEntry
    var isnigt = getnightmodeflag()

    @Environment(\.widgetFamily) var widgetType
    var body: some View {
        if #available(iOSApplicationExtension 16.0, *) {
            switch widgetType {
            case .accessoryRectangular:
                getMainViewForLockScreenWidgetWith(entry: myDataEntry)
            case .systemMedium:
                GeometryReader { geo in
                    VStack (alignment: .leading) {
                        VStack(alignment: .leading,spacing: 0) {
                        // My UI Stuff
                        }
                        .padding(0)
                    }
                }
            default:
                Text("No Data Available")
            }
        }
        else {
            switch widgetType {
            case .systemMedium:
                GeometryReader { geo in
                    VStack (alignment: .leading) {
                        VStack(alignment: .leading,spacing: 0) {
                        // My UI Stuff
                        }
                        .padding(0)
                    }
                }
            default:
                Text("No Data Available")
            }
        }
    }
}

Am I doing something wrong? Let me know if any more explanation needed

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
Mubin Mall
  • 546
  • 10
  • 25

1 Answers1

1

Yes, You need to Configuration for both type of widgets.

You can set separate Configuration and WidgetBundle

@main
struct WidgetNewEntryView: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        MediumWidget()
        LockScreenWidget()
    }
}

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

    var body: some WidgetConfiguration {
      // You Configuration here
        .supportedFamilies([.systemMedium]) // <--- Here
    }
}

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

    var body: some WidgetConfiguration {
      // You Configuration here
        .supportedFamilies([.accessoryCircular]) // <--- Here
    }
}



struct MediumWidgetView : View {
    var myDataEntry: DataEntry
    var isnigt = getnightmodeflag()

    @Environment(\.widgetFamily) var widgetType
    var body: some View {
        switch widgetType {
        case .systemMedium:
            GeometryReader { geo in
                VStack (alignment: .leading) {
                    VStack(alignment: .leading,spacing: 0) {
                    // My UI Stuff
                    }
                    .padding(0)
                }
            }
        default:
            Text("No Data Available")
        }
    }
}


struct LockScreenWidget : View {
    var myDataEntry: DataEntry
    var isnigt = getnightmodeflag()

    @Environment(\.widgetFamily) var widgetType
    var body: some View {
        if #available(iOSApplicationExtension 16.0, *) {
            switch widgetType {
            case .accessoryRectangular:
                getMainViewForLockScreenWidgetWith(entry: myDataEntry)
            default:
                Text("No Data Available")
            }
        }
        else {
            EmptyWidgetConfiguration()
        }
    }
}
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121