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

如何使用Siri Kit添加App Intent,实现语音唤起转账APP至指定页面?

为转账APP添加Siri语音唤起并跳转指定页面的解决方案

问题背景

我有一款转账APP,希望添加Siri支持,实现说出“Hey Siri,Send Money”时,若APP未运行则打开APP并跳转至指定页面。已尝试添加App Intents Extension并编写了如下代码:

import AppIntents

struct SendMoney: AppIntent {
    static var title: LocalizedStringResource = "SendMoney"
    static var openAppWhenRun: Bool = true
    func perform() async throws -> some IntentResult {
        return .result()
    }
}

struct MyAppShortcuts: AppShortcutsProvider{
    static var appShortcuts: [AppShortcut]{
        AppShortcut(intent: SendMoney(), phrases: ["Transfer Money in DemoApp"])
    }
}

优化方案

当前代码仅能实现打开APP的基础功能,要满足语音唤起并跳转指定页面的需求,需从以下几处调整:

1. 调整Siri触发短语

修改AppShortcuts中的phrases,添加贴合需求的自然语音指令,确保Siri能识别“Hey Siri, Send Money”:

struct MyAppShortcuts: AppShortcutsProvider{
    static var appShortcuts: [AppShortcut]{
        AppShortcut(
            intent: SendMoney(),
            phrases: [
                "Send Money in DemoApp",
                "Send Money with DemoApp",
                "Hey Siri, Send Money"
            ],
            shortTitle: "Send Money",
            systemImageName: "arrow.up.circle"
        )
    }
}

2. 传递跳转标识并处理APP内跳转

在Intent的perform方法中生成自定义NSUserActivity,用于APP启动后识别跳转目标;同时在APP的场景代理中处理跳转逻辑:

调整Intent代码

struct SendMoney: AppIntent {
    static var title: LocalizedStringResource = "Send Money"
    static var openAppWhenRun: Bool = true
    
    func perform() async throws -> some IntentResult {
        // 创建自定义用户活动,标记跳转动作
        let sendMoneyActivity = NSUserActivity(activityType: "com.yourapp.sendMoney")
        sendMoneyActivity.userInfo = ["targetPage": "sendMoney"]
        
        return .result()
            .userActivity(sendMoneyActivity)
    }
}

APP端处理跳转

在SceneDelegate(iOS 13+)中添加用户活动处理逻辑:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // 处理启动时的用户活动
    if let activity = connectionOptions.userActivities.first ?? session.stateRestorationActivity {
        handleIntentActivity(activity)
    }
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    // 处理APP后台唤起时的用户活动
    handleIntentActivity(userActivity)
}

private func handleIntentActivity(_ activity: NSUserActivity) {
    guard activity.activityType == "com.yourapp.sendMoney",
          let targetPage = activity.userInfo?["targetPage"] as? String,
          targetPage == "sendMoney",
          let window = (scene as? UIWindowScene)?.windows.first else {
        return
    }
    
    // 跳转到指定转账页面
    let sendMoneyVC = SendMoneyViewController() // 替换为你的转账页面视图控制器
    if let navController = window.rootViewController as? UINavigationController {
        navController.pushViewController(sendMoneyVC, animated: true)
    } else {
        window.rootViewController = UINavigationController(rootViewController: sendMoneyVC)
    }
}

3. 配置Info.plist

  • 在主APP的Info.plist中添加NSUserActivityTypes数组,包含自定义活动类型:
    <key>NSUserActivityTypes</key>
    <array>
        <string>com.yourapp.sendMoney</string>
    </array>
    
  • 确保App Intents Extension的Info.plist中,NSExtensionAttributes下的IntentsSupported包含SendMoney:
    <key>NSExtension</key>
    <dict>
        <key>NSExtensionAttributes</key>
        <dict>
            <key>IntentsSupported</key>
            <array>
                <string>SendMoney</string>
            </array>
        </dict>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.intents-service</string>
    </dict>
    

4. 测试注意事项

  • 必须在真机上测试Siri功能,模拟器无法完整支持语音唤起
  • 确保已在系统设置中为APP开启Siri权限
  • 首次使用需通过APP内的快捷方式设置,或手动在Siri设置中添加该指令

内容的提问来源于stack exchange,提问作者Vishal Thakur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 15:45:34