AppIntent @Dependency Throwing Error

I'm not really sure if I'm using the right lingo here because there's so little documentation on this, so apologies in advance.

I have an app with a few custom intents that I'm attempting to transition to AppIntents. I have the newly transitioned intents showing up in the Shortcuts app as expected, however when I run them I get an immediate failure saying "The operation couldn't be completed" (see photo). Note that the "AppIntentsClient" class mentioned in the photo is the dependency I'm trying to import.

I've narrowed it down to the @Dependency that I'm using in my intent handler. At the top of the intent handler I have a line:

@Dependency
private var appIntentsClient: any AppIntentsClient // NOTE: AppIntentsClient is a protocol -- could that be the issue?

And if I comment out this line, the intent no longer throws that error.

I'm following the guidelines shown in sample apps by setting the dependency on my main app's startup in didFinishLaunchingWithOptions like so:

// gets called by the main app `didFinishLaunchingWithOptions`
func onDidFinishLaunching() {
    let adapter = AppIntentsAdapter() //AppIntentsAdapter adheres to protocol AppIntentsClient
    self.appIntentsAdapter = adapter
    AppDependencyManager.shared.add(dependency: adapter)
    
    MyAppShortcuts.updateAppShortcutParameters()
}

Unfortunately there is virtually no documentation around AppDependencyManager or AppDependencies in general. Both documentation pages have at most one line, but don't indicate why this would be failing for me. Is there any information out there on why these errors may be happening? I've also looked at the Console app to see if the OS logs anything, but nothing of value was found.

https://developer.apple.com/documentation/appintents/appdependencymanager

https://developer.apple.com/documentation/appintents/appdependency

Answered by wilc0 in 801999022

@DTS Engineer I actually figured out the issue. The problem was using a protocol for the dependency. Once I switched it to a concrete class, the error went away.

Also, using SwiftUI lifecycle FWIW

There's an example of using @Dependency in the Trails sample code project using the SwiftUI app lifecycle. Does that example work for your app? Since you're mentioning didFinishLaunchingWithOptions, I'm unclear if you're using the SwiftUI app lifecycle with a delegate adapter, or if your app is using UIKit without SwiftUI.

— Ed Ford,  DTS Engineer

Accepted Answer

@DTS Engineer I actually figured out the issue. The problem was using a protocol for the dependency. Once I switched it to a concrete class, the error went away.

Also, using SwiftUI lifecycle FWIW

AppIntent @Dependency Throwing Error
 
 
Q