如何在应用关闭时使用动态快速操作 [swift]

How to use dynamic quick action when app is closed [swift]

如何在swift中使用动态快捷操作? 如果应用程序处于多任务处理状态,我可以触发我的方法。但如果应用程序不在后台 运行,它就无法运行。 我正在 applicationWillResignActive / sceneWillResignActive 中设置我的快速操作。 我正在处理我的方法: “func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void)” 和
“func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void)”

有什么建议吗?

performActionFor 方法仅在应用程序处于 运行 时调用(在 background0 中。如果不是,则需要在应用程序启动时在 didFinishLaunchingWithOptions 或 willFinishLaunchingWithOptions 中检查快捷方式。快捷方式项将是在启动项中。

像这样:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    if let shotcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem{
        handleShortcutItem(shortcutItem)
    }

    return true
}

显然 handleShortcutItem 是一种实际处理快捷方式并执行您想要的操作的方法。对于这两种情况,您应该可以使用同一个。