我计划在开发的LockLive应用里添加捷径快捷方式,命名为 LockNow,描述为 LockLive Standby Mode,整理一下如下:

先上代码

新建一个 Siri Intent Definition File 类型文件

  1. Intents.intentdefinition
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>INEnums</key>
	<array/>
	<key>INIntentDefinitionModelVersion</key>
	<string>1.2</string>
	<key>INIntentDefinitionNamespace</key>
	<string>HF1gjA</string>
	<key>INIntentDefinitionSystemVersion</key>
	<string>24E263</string>
	<key>INIntentDefinitionToolsBuildVersion</key>
	<string>16E140</string>
	<key>INIntentDefinitionToolsVersion</key>
	<string>16.3</string>
	<key>INIntents</key>
	<array>
		<dict>
			<key>INIntentCategory</key>
			<string>generic</string>
			<key>INIntentConfigurable</key>
			<true/>
			<key>INIntentDescription</key>
			<string>LockLive Standby Mode</string>
			<key>INIntentDescriptionID</key>
			<string>3CFvai</string>
			<key>INIntentManagedParameterCombinations</key>
			<dict>
				<key></key>
				<dict>
					<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
					<true/>
					<key>INIntentParameterCombinationUpdatesLinked</key>
					<true/>
				</dict>
			</dict>
			<key>INIntentName</key>
			<string>Intent</string>
			<key>INIntentParameterCombinations</key>
			<dict>
				<key></key>
				<dict>
					<key>INIntentParameterCombinationIsPrimary</key>
					<true/>
					<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
					<true/>
				</dict>
			</dict>
			<key>INIntentResponse</key>
			<dict>
				<key>INIntentResponseCodes</key>
				<array>
					<dict>
						<key>INIntentResponseCodeName</key>
						<string>success</string>
						<key>INIntentResponseCodeSuccess</key>
						<true/>
					</dict>
					<dict>
						<key>INIntentResponseCodeName</key>
						<string>failure</string>
					</dict>
				</array>
			</dict>
			<key>INIntentTitle</key>
			<string>LockNow</string>
			<key>INIntentTitleID</key>
			<string>GFaSHZ</string>
			<key>INIntentType</key>
			<string>Custom</string>
			<key>INIntentVerb</key>
			<string>Do</string>
		</dict>
	</array>
	<key>INTypes</key>
	<array/>
</dict>
</plist>

新建两个Swift源代码类型文件

  1. LockNowIntent.swift
  2. LockNowShortcuts.swift

//  LockNowIntent.swift

import AppIntents

struct LockNowIntent: AppIntent {
    static var title: LocalizedStringResource = "LockNow"
    static var description = IntentDescription("LockLive Standby Mode.")
    static var openAppWhenRun: Bool = true

    @MainActor // Ensures perform() runs on the main thread
    func perform() async throws -> some IntentResult & ReturnsValue<Bool> {
        print("LockNowIntent")
        return .result(value: true)
    }
}

//
//  LockNowShortcuts.swift

import AppIntents

struct LockNowShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: LockNowIntent(),
            phrases: [
                "Open \(.applicationName)",  // 必须有\(.applicationName)"
                "Launch \(.applicationName)",
                "\(.applicationName)"
            ],
            shortTitle: "Lock Now", // 显示在捷径中快捷方式的标题
            systemImageName: "bolt.circle" // 显示在捷径中快捷方式的图标
        )
    }
}

客官稍等,下面是DeepSeek给我的开发建议,基本上是错的。

在 Xcode 16 中开发 Intent 以实现通过 Siri 快捷指令快速打开自己开发的 App,可以按照以下步骤操作。这些步骤结合了最新的 App Intents 框架(适用于 iOS 16+ 和 macOS Ventura+),并参考了相关技术文档和实践经验。


1. 配置项目以支持 App Intents

确保你的项目满足以下条件:

  • Xcode 16:支持最新的 App Intents API。
  • iOS 16+/macOS Ventura+:App Intents 需要较新的系统版本。
  • 添加 Intent Definition 文件
    1. 在 Xcode 项目中,选择 File > New > File
    2. 搜索 “Siri Intent Definition File” 并创建。
    3. .intentdefinition 文件中定义你的 Intent。

2. 定义 App Intent

.intentdefinition 文件中,创建一个新的 Intent:

  • Intent 类型:选择 “Open App” 或自定义操作。
  • 参数(可选):可以添加如 deepLink 参数以支持特定页面跳转。
  • 标题和描述:如 "Open My App""Quickly launch MyApp from Shortcuts."

3. 实现 App Intent 代码

在 Swift 中,创建一个符合 AppIntent 协议的结构体:

import AppIntents

struct OpenMyAppIntent: AppIntent {
    static var title: LocalizedStringResource = "Open My App"
    static var description = IntentDescription("Opens MyApp directly from Shortcuts.")

    func perform() async throws -> some IntentResult {
        // 使用 Deeplink 或直接打开 App
        let url = URL(string: "myapp://")!
        if await UIApplication.shared.canOpenURL(url) {
            await UIApplication.shared.open(url)
            return .finished()
        } else {
            throw NSError(domain: "AppNotInstalled", code: 404)
        }
    }
}

4. 注册 Intent 并添加快捷短语

使用 AppShortcutsProvider 让 Shortcuts 应用识别你的 Intent:

struct MyAppShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: OpenMyAppIntent(),
            phrases: ["Open \(.applicationName)"],
            shortTitle: "Launch My App",
            systemImageName: "app"
        )
    }
}
  • phrases:用户可以说或输入的快捷指令短语,如 "Open MyApp"

5. 处理 Intent 回调

AppDelegate 或 SwiftUI 的入口处,确保处理 Intent 的返回:

// 对于 SwiftUI
@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

// 对于 UIKit
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    if userActivity.activityType == "OpenMyAppIntent" {
        // 处理 Deeplink 逻辑
        return true
    }
    return false
}

6. 测试与调试

  1. 在 Xcode 中运行:确保 Intent 出现在 Shortcuts 应用中。
  2. 手动测试
    • 打开 Shortcuts App,创建新快捷指令。
    • 搜索你的 App 名称,应能看到定义的 Intent。
    • 运行快捷指令,检查是否能正确打开 App。

如果 Intent 未显示,尝试:

  • 清理 Xcode 派生数据(Xcode > Preferences > Locations > DerivedData)。
  • 重启设备和 Shortcuts App。

7. 进阶功能(可选)

  • 支持旧系统(iOS 15 及以下):使用 NSUserActivitySiriKit Intents 作为备用方案。
  • 多语言支持:在 .intentdefinition 文件中配置本地化字符串。
  • 参数化 Deeplink:如 myapp://home?tab=settings,以支持更多场景。

总结

通过上述步骤,你可以在 Xcode 16 中实现一个简单的 App Intent,让用户通过 Siri 或快捷指令快速打开你的 App。如需更复杂的功能(如传递参数或执行特定操作),可进一步扩展 Intent 逻辑。