Is there any way to expose an AppIntent that lives in the app package or an AppIntentExtension to a widget?

I have AppIntent code which lives in my app package that can not easily be decoupled and migrated into its own standalone framework or extension. This is because this intent has some action that relies on my app's network and core data stack. I'd like to expose this existing intent in some way to a Button in a widget, but so far I have not had success. Is this currently possible with AppIntentPackage or some other means? Is there a way I can use AppIntents to pass a message back to the app? How should I approach this?

Thank you!

The documentation states you can add it to the app target and it’ll run in your app’s process. But I don’t see how it would compile when referencing an intent in the widget extension when that intent struct only exists in the app target. 🤔 Were you able to figure it out?

I ran some tests and documented what I’m seeing: https://stackoverflow.com/a/76673642/1795356

I'm also facing the same issue. I have AppIntents as they require access to my core data stack, network for syncing, notification scheduling, starting live activities etc. But I can't find any way to use an AppIntent from a WidgetExtension without having to add almost my entire apps code base to the WidgetExtension.

This seems backwards, why create an AppIntent which runs in the Apps process if you have to put all the code into the WidgetExtension? I must be missing something, the docs make it sound easy to use them but lack details on how it should be setup.

I found a better solution for interaction Live Activity.

  1. You can create a struct impletion LiveActivityIntent then add it to widget target and app target.

  2. Create two files, one add to widget target, other one add to app target, each extension your Intent and write one same function. Widget target function can empty, app target function is real code. In perform call your function in extensions file.

Above steps make sure compile success and when interaction intent, System will execute your real code in App Process.

Can confirm @blacksun method works. I created the same AppIntent twice, but with an empty perform block in the one I added to the WidgetExtensions target. The one in my main app target has the fully implemented Perform function and is the one called at run time.

@fehners My solution works with LiveActivityIntent because it will perform in app process.

one more thing, intent struct must be marked public, else in release mode(debug mode is well), system cannot find your intent.

Is there any way to expose an AppIntent that lives in the app package or an AppIntentExtension to a widget?
 
 
Q