Thursday, April 18, 2024
HomeiOS Developmentios - Configurable Widget not working in Swift Package deal

ios – Configurable Widget not working in Swift Package deal


My iOS App has two static widgets. The code for the widgets resides in a library that’s a part of a Swift Package deal, imported into the primary venture.
The 2 static widgets work as anticipated, but when I add a configurable widget, following the identical strategy, this one doesn’t work and solely shows the placeholder view.

In additional element, within the widget extension, I’ve the next code for the 2 static widgets:

import WidgetKit
import SwiftUI
import ItemWidgetsKit

@major
struct ItemWidgetsBundle: WidgetBundle {
    var physique: some Widget {
        RecentSavesWidget()
        RecommendationsWidget()
    }
}

the remainder of the code, together with the 2 Widget implementations, the TimelineProvider, and so on, are within the imported ItemsWidgetKit, which is the aforementioned library a part of a Swift Package deal. These two widgets work as anticipated.

So as to add a configurable widget, the code above modifications like so:

import WidgetKit
import SwiftUI
import ItemWidgetsKit

@major
struct ItemWidgetsBundle: WidgetBundle {
    @WidgetBundleBuilder var physique: some Widget {
        makeWidgets()
    }

    func makeWidgets() -> some Widget {
        if #obtainable(iOS 17.0, *) {
            return WidgetBundleBuilder.buildBlock(RecentSavesWidget(), TopicRecommendationsWidget())
        } else {
            return WidgetBundleBuilder.buildBlock(RecentSavesWidget(), RecommendationsWidget())
        }
    }
}

TopicRecommendationsWidget is just like RecommendationsWidget besides that it helps you to select a subject to show suggestions for, by lengthy urgent and choosing an intent. It makes use of the identical logic as RecommendationsWidget to learn information from a file, written by the primary app.
I adopted the identical strategy of the opposite two widgets, and put the code inside ItemWidgetsKit This time, although, the widget doesn’t work, and it solely hundreds the placeholder; in actual fact, within the AppIntentTimelineProvider implementation (see under) solely placeholder(in:) will get ever known as

@obtainable(iOS 17.0, *)
public struct TopicTimelineProvider: AppIntentTimelineProvider {
    public func placeholder(in context: Context) -> TopicEntry {
        return TopicEntry(date: Date(), content material: TopicContent.sampleContent)
    }

    public func snapshot(for configuration: TopicIntent, in context: Context) async -> TopicEntry {
        return TopicEntry(date: Date(), content material: configuration.topicEntity.subject)
    }

    public func timeline(for configuration: TopicIntent, in context: Context) async -> Timeline<TopicEntry> {
        let entry = TopicEntry(date: Date(), content material: configuration.topicEntity.subject)

      return Timeline(entries: [entry], coverage: .by no means)
    }
}

for extra context, here is the code for the configurable Widget implementation (that resides inside ItemWidgetsKit

@obtainable(iOS 17.0, *)
public struct TopicRecommendationsWidget: Widget {
    let sort: String

    public init() {
        self.sort = WidgetKind.topicRecommendations // a string saved elsewhere
    }

    public var physique: some WidgetConfiguration {
        AppIntentConfiguration(sort: sort, intent: TopicIntent.self, supplier: TopicTimelineProvider()) { entry in
            TopicWidgetContainerView(entry: entry)
        }
        .configurationDisplayName("New Suggestions")
        .description("Choose a Subject")
        .supportedFamilies([.systemMedium, .systemLarge])
    }
}

if I transfer the code out of the Swift Package deal and add it on to the widget extension goal, then it really works as anticipated, however this may disrupt the present structure.

Any Thought why this occurs and what could be the answer to this drawback? Thanks



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments