使用AppIntents选择AppEnum时Widget未更新问题排查
问题原因及修复方案
核心问题
你的代码里没有从配置对象TestIntent中读取枚举值,而是在timeline和snapshot方法里硬编码成了固定的TestEnum.test1,导致修改枚举配置时,Widget无法获取到最新的选项值,自然不会更新。而布尔值和字符串因为正确读取了configuration里的对应属性,所以能正常更新。
具体修复步骤
1. 修复timeline方法
把硬编码的TestEnum.test1替换为从配置中读取的枚举值:
func timeline(for configuration: TestIntent, in context: Context) async -> Timeline<TestEntry> { let testBool = configuration.bool let testStr = configuration.str let testEnum = configuration.enum1 // 读取配置里的枚举选项 return Timeline(entries: [ TestEntry( date: .now, bool: testBool, str: testStr, enum1: testEnum // 使用最新的枚举配置值 ) ], policy: .atEnd) }
2. 修复snapshot方法
同样,snapshot方法也要使用配置中的枚举值,不能硬编码:
func snapshot(for configuration: TestIntent, in context: Context) async -> TestEntry { return TestEntry( date: .now, bool: configuration.bool, str: configuration.str, enum1: configuration.enum1 // 同步配置里的枚举值 ) }
额外说明
你当前的TestEnum实现是符合Apple官方规范的,配置界面的下拉选项显示逻辑没问题,只需要修正上述两个方法中枚举值的读取逻辑,就能让Widget跟随枚举配置更新了。
内容的提问来源于stack exchange,提问作者Cécile Lebleu
相关产品推荐
相关产品推荐

