How to use dynamic quick action in iOS?
I am able to trigger my methods if the app is in multitasking. But if the app isn't running in the background then it doesn't work.
I am setting up my quick actions in applicationWillResignActive
/ sceneWillResignActive
.
I am handling my methods in:
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void)
and
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void)
Any suggestions?
The performActionFor method is only called when the app is running (in the background0. If it is not you need to check for shortcuts when the app starts in the didFinishLaunchingWithOptions or willFinishLaunchingWithOptions. The shortcut item will be in the launch items.
Something like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem{
handleShortcutItem(shortcutItem)
}
return true
}
Obviously the handleShortcutItem
is a method that will actually process the shortcut and do what you want. You should be able to use the same one for both cases.