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 -> 新建快捷指令 -> 搜索项目名称

运行效果

相关推荐
他们都不看好你,偏偏你最不争气8 小时前
【iOS】UIViewController
开发语言·macos·ios·objective-c·cocoa
前端小超超13 小时前
如何配置capacitor 打包的ios app固定竖屏展示?
前端·ios·web app
CocoaKier13 小时前
AI让35岁程序员再次伟大
ios·微信小程序·aigc
库奇噜啦呼13 小时前
【iOS】单例模式
ios·单例模式
2501_9159090613 小时前
苹果上架App软件全流程指南:iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传与审核技巧详解
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214313 小时前
iOS 文件管理与能耗调试结合实战 如何查看缓存文件、优化电池消耗、分析App使用记录(uni-app开发与性能优化必备指南)
android·ios·缓存·小程序·uni-app·iphone·webview
2501_9159184114 小时前
App 苹果 上架全流程解析 iOS 应用发布步骤、App Store 上架流程
android·ios·小程序·https·uni-app·iphone·webview
库奇噜啦呼14 小时前
【iOS】UIViewController生命周期
macos·ios·cocoa
2501_9160074714 小时前
苹果上架全流程详解,iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传与审核要点完整指南
android·ios·小程序·https·uni-app·iphone·webview
YungFan15 小时前
iOS26适配指南之UISlider
ios·swift