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

如何动态更新watchOS表盘组件的AppIntentRecommendation列表

watchOS 表盘组件推荐列表不更新问题

我的watch应用中,func recommendations()返回的AppIntentRecommendation需要在应用启动后完成更新。

问题详情:应用首次安装时,生成AppIntentRecommendation列表所需的信息不可用,因此初始返回一个“占位”的AppIntentRecommendation。用户首次启动应用并登录后,func recommendations()中的AppIntentRecommendation已更新,但watchOS并未同步更新表盘上展示的组件推荐列表。

当前代码实现

func recommendations() -> [AppIntentRecommendation<ConfigurationAppIntent>] {
    // 创建所有预配置组件的展示列表
    let defaults = UserDefaults.init(suiteName: "group.myApp")!
    guard let names = defaults.dictionary(forKey: "names") as? [String:String] else {
        return [AppIntentRecommendation(intent: .Demo, description: "Demo")]
    }

    var recs = [AppIntentRecommendation<ConfigurationAppIntent>]()
    for (idx, name) in names {
        let rec = ConfigurationAppIntent()
        rec.order = idx
        rec.carName = name
        rec.action = nil
        recs.append(AppIntentRecommendation(intent: rec, description: "name") )
    }
    return recs
}

解决方法

watchOS不会自动监听UserDefaults的变化来更新组件推荐,需要主动触发刷新逻辑:

  • 主动调用刷新API:在用户登录完成、UserDefaults中的names数据更新后,立即调用AppIntent.refreshRecommendations(),通知系统重新拉取推荐列表:

    // 登录成功并更新UserDefaults后执行此代码
    AppIntent.refreshRecommendations()
    
  • 校验App Group配置:确保主应用与watch扩展已正确配置相同的App Group标识,避免跨组件的UserDefaults数据不同步。

  • 保证Intent实例唯一性:确保生成的ConfigurationAppIntent实例参数(如order、carName)能唯一标识每个推荐项,避免系统判定推荐内容无变化而跳过更新。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 16:24:51