iOS Intents Extension Hello World Demo

本文记录使用 Intents Extension 实现一个自定义弹框的 Hello World 案例。

1. 创建 Intents Extension

选中项目根目录 -> Xcode 顶部 File -> New -> Target...

搜索 Intents

选择 Intents Extension,然后 Next

勾选 Include UI Extension 因为要自定义UI,Finish 后选择 Activate

2. 创建 SiriKit Intent Definition File

选中项目根目录 -> 右键 -> New File... -> 搜索 Siri -> 选中 SiriKit Intent Definition File -> Next -> Create

3. 新建 Intent

打开刚才创建的 SiriKit Intent Definition File,文件后缀为 .intentdefinition

点左下角的 + -> new Intent

这里命名为 HelloIntent,然后选中 HelloIntent,输入 Tittle、Description

Target Membership 需勾选上 HelloWorldIntent 和 HelloWorldIntentUI

创建好之后运行一下(快捷键 command + B)会自动生成 Intents 的依赖文件

4. 修改 Intents Extension 生成的文件

假如创建的 Intents Extension 命名为 HelloWorldIntent,在项目中会创建两个文件夹:HelloWorldIntentHelloWorldIntentUI

先修改 HelloWorldIntent 下的 IntentHandler 文件:

swift 复制代码
import Intents

class IntentHandler: INExtension, HelloIntentIntentHandling {

    func handle(intent: HelloIntentIntent, completion: @escaping (HelloIntentIntentResponse) -> Void) {

        // 创建成功响应

        let response = HelloIntentIntentResponse(code: .success, userActivity: nil)

        completion(response)

    }
}

修改 HelloWorldIntentUI 下的 IntentViewController 文件:

swift 复制代码
import IntentsUI

class IntentViewController: UIViewController, INUIHostedViewControlling {
    // 需在 MainInterface 中创建并关联
    @IBOutlet var valueLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        print("======viewDidLoad被调用====")
        view.backgroundColor = .red  // 直接改成红色背景
        // 确保valueLabel已正确初始化
        if valueLabel != nil {
            valueLabel.text = "初始化成功"
            print("valueLabel已初始化")
        } else {
            print("valueLabel未初始化")
        }
    }

    // 必须实现这个方法,不然自定义 UI 可能不加载
    func configureView(for parameters: Set<INParameter>, of interaction: INInteraction, interactiveBehavior: INUIInteractiveBehavior, context: INUIHostedViewContext, completion: @escaping (Bool, Set<INParameter>, CGSize) -> Void) {
        self.valueLabel.text = "hello world"
        completion(true, Set(), self.desiredSize)
    }

    var desiredSize: CGSize {
        let width = self.extensionContext?.hostedViewMaximumAllowedSize.width ?? 320
        return CGSize.init(width: width, height: 150)
    }

}

在 MainInterface 中创建视图并关联字段,选中 Intent View Controller

点击右上角的 + 选择需要的组件,拖拽到视图中

关联组件:右上角切换到 Show the connections inspector -> 按住 Referencing Outlets 下的小圆点不松开 -> 拉到新建的 Label 组件上

关联后的效果

5. 修改 Info.plist

分别修改 HelloWorldIntent 和 HelloWorldIntentUI 的 Info.plist,与 Intent 关联上

6. 运行

选中主目录,然后直接运行

打开快捷指令app -> 新建快捷指令 -> 搜索项目名称

运行效果

相关推荐
2501_916008892 天前
iOS开发APP上架全流程解析:从开发到App Store的完整指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915909062 天前
Charles 抓不到包怎么办?iOS 调试过程中如何判断请求路径
android·ios·小程序·https·uni-app·iphone·webview
2501_916007472 天前
iOS和iPadOS文件管理系统全面解析与使用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915921433 天前
iOS App 开发阶段性能优化,观察 CPU、内存和日志变化
android·ios·性能优化·小程序·uni-app·iphone·webview
游戏开发爱好者83 天前
在 iOS 开发、测试与上架过程中 如何做证书管理
android·ios·小程序·https·uni-app·iphone·webview
ii_best3 天前
按键精灵安卓/IOS手机助手 × 手机按键 App:1 分钟搞定设备连接(超详细教程)
android·ios·智能手机·自动化·编辑器
2501_916007473 天前
在没有 Mac 的情况下完成 iOS 应用上架 App Store
android·macos·ios·小程序·uni-app·iphone·webview
TheNextByte13 天前
iPhone存储空间已满?如何轻松释放iPhone空间?
android·ios·iphone
—Qeyser3 天前
Flutter 颜色完全指南
android·flutter·ios
2501_916008893 天前
iOS 上架需要哪些准备,账号、Bundle ID、证书、描述文件、安装测试及上传
android·ios·小程序·https·uni-app·iphone·webview