iOS 17+交互式Widget中AppIntent完成块网络请求处理问题
解决方案
问题根源
自定义IntentResult类型不被系统支持,这正是你遇到「Unsupported IntentResult type」崩溃的核心原因。iOS的AppIntent框架仅允许使用系统提供的IntentResult实现,无法自行扩展或自定义该协议的类型。
修复步骤
删除自定义的
NetworkRequestResult结构体
直接移除你编写的这个自定义类型,不需要自行实现IntentResult协议。修改
perform()方法,使用系统原生的IntentResult
保留withCheckedThrowingContinuation桥接完成块的逻辑,调整返回结果为系统提供的原生类型:func perform() async throws -> some IntentResult { return try await withCheckedThrowingContinuation { continuation in fetcher.trigger() { result in switch result { case .success: // 返回系统原生成功结果,可附带用户可见提示消息 continuation.resume(returning: .success(message: "请求成功")) case .failure(let error): // 抛出错误,系统会自动转换为失败状态的IntentResult continuation.resume(throwing: error) } } } }
补充说明
- 若不需要显示用户可见的成功提示,直接返回
.success()即可:continuation.resume(returning: .success()) - 之前使用
.result()编译报错,是因为系统的IntentResult无需手动调用.result(),直接使用.success()或抛出错误就能生成符合要求的返回值 - 确保抛出的错误是系统可识别的类型(比如
NSError或自定义Error实现),这样Widget才能正确处理失败状态
内容的提问来源于stack exchange,提问作者Paulo Silva
相关产品推荐
相关产品推荐

