Mubin Mall
Mubin Mall

Reputation: 546

iOS 16 Lock Screen Widget along with Home Screen Widget

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

Upvotes: 1

Views: 1633

Answers (1)

Toseef Khilji
Toseef Khilji

Reputation: 17409

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()
        }
    }
}

Upvotes: 2

Related Questions