Mattias Farnemyhr
Mattias Farnemyhr

Reputation: 4238

How to setup an iOS 17 interactive Widget using AppIntents from an SPM package?

I've setup an interactive Widget in iOS 17 which uses an AppIntent from an SPM package but nothing works when you tap on the button in the widget.

Supposedly you should be able to use AppIntentsPackage, which should make it possible to use AppIntents from other frameworks/SPM package(?), but I cannot get it to work. This is my setup:

SPMPackage

public struct MyAppIntent: AppIntent { ... }

public struct MyAppIntentPackage: AppIntentsPackage { }

WidgetExtension

import SPMPackage

struct MyWidgetEntryView: View {
    var body: some View {
        Button(intent: MyAppIntent()) {
            Text("Tap here")
        }
        .containerBackground(...)
    }
}

extension MyWidgetBundle: AppIntentsPackage {
    static var includedPackages: [AppIntentsPackage.Type] = [
        MyAppIntentPackage.self
    ]
}

Seems to have set it up exactly how Apple explains it in the documentation but it doesn't work. Am I missing something? Thanks!

Upvotes: 4

Views: 1219

Answers (2)

izmatlDev
izmatlDev

Reputation: 13

I had the same issue, and I think this is not supported by Apple. So what I do was moved out all things related to WidgetKit from my package, and put it on the main project that consumes the package and I pass intents by dependency injection where it is necessary.

Upvotes: 0

Mattias Farnemyhr
Mattias Farnemyhr

Reputation: 4238

Got word back from an Apple representative that having AppIntents in an SPM package is not yet supported.

I solved my issue by moving all the code related to AppIntents in to the WidgetExtension and now it works reliably.

Hope this helps someone else in the future, but more hopeful that this will be resolved in a future iOS version.

Upvotes: 7

Related Questions