You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

静态Quick Action点击无响应:未触发App函数的排查求助

静态Quick Action点击无响应问题排查

已配置的静态Quick Action

UIApplicationShortcutItemIconSymbolName = "alarm"
UIApplicationShortcutItemType = "com.yocto.xyz.action"
UIApplicationShortcutItemTitle = "Hello"

未触发的相关代码

AppDelegate 代码片段

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        print(">>>>> AppDelegate didFinishLaunchingWithOptions")
        return true
    }

    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        print(">>>>> AppDelegate shortcutItem.type \(shortcutItem.type)")
        completionHandler(true)
    }

    // ...
}

SceneDelegate 代码片段

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
        print(">>>>> SceneDelegate willConnectTo")
    }

    // ...
}

点击配置的Quick Action后,上述两类中的相关方法均未执行,问题可通过Demo项目复现。


问题原因及解决方法

1. SceneDelegate 时代的逻辑变更

iOS 13+ 采用SceneDelegate架构的项目中,AppDelegate的application(_:performActionFor:completionHandler:)方法不再会被调用,系统会将Quick Action事件直接传递给SceneDelegate,这是核心原因。

2. 补充SceneDelegate的处理方法

需要在SceneDelegate中实现以下两个方法,覆盖冷启动和后台唤起两种场景:

(1)处理冷启动时的Quick Action

在scene(_:willConnectTo:options:)中读取启动参数里的快捷操作:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }
    print(">>>>> SceneDelegate willConnectTo")
    
    // 冷启动时处理快捷操作
    if let shortcutItem = connectionOptions.shortcutItem {
        handleShortcutItem(shortcutItem)
    }
}

(2)处理后台唤起时的Quick Action

实现windowScene(_:performActionFor:completionHandler:)方法:

func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    print(">>>>> SceneDelegate shortcutItem.type \(shortcutItem.type)")
    handleShortcutItem(shortcutItem)
    completionHandler(true)
}

(3)统一处理逻辑封装

可新增工具方法统一处理不同类型的快捷操作:

private func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) {
    switch shortcutItem.type {
    case "com.yocto.xyz.action":
        // 执行对应业务逻辑
        print("触发Hello快捷操作")
    default:
        break
    }
}

3. 验证Info.plist配置结构

确保静态Quick Action是添加在UIApplicationShortcutItems数组下,而非单独的键值对,正确结构如下:

<key>UIApplicationShortcutItems</key>
<array>
    <dict>
        <key>UIApplicationShortcutItemIconSymbolName</key>
        <string>alarm</string>
        <key>UIApplicationShortcutItemType</key>
        <string>com.yocto.xyz.action</string>
        <key>UIApplicationShortcutItemTitle</key>
        <string>Hello</string>
    </dict>
</array>

4. 测试注意事项

  • 冷启动测试:完全退出App后,通过3D Touch/Haptic Touch触发Quick Action
  • 后台测试:将App切换到后台后触发Quick Action,验证windowScene(_:performActionFor:completionHandler:)是否调用

内容的提问来源于stack exchange,提问作者Cheok Yan Cheng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 23:03:25