@UTSJS.keepAlive
class WebViewOptionsAdapter implements IOSVoiceWebViewOptions {
// 参数
private options : VoiceOptions | null = null
// 构造
constructor(options : VoiceOptions | null) {
this.options = options
}
// 首次进入语音
get firstStartVoice() : boolean {
return this.options?.firstStartVoice ?? false
}
// 语音模式切换回调
onVoiceModeChange(status : boolean) : void {
if (this.options != null) {
this.options.onVoiceModeChange(status)
}
}
// 语音页面加载状态回调
// Swift 协议要求 Int 类型,但 UTS 接口需要 number 类型,需要转换
@UTSJS.keepAlive
onLoadStatus(mode : Int) : void {
if (this.options != null) {
console.log('加载模式', mode)
this.options.onLoadStatus(mode as number)
}
}
// 模型切换回调
onChangeModelCallback(status : boolean) : void {
if (this.options != null) {
this.options.onChangeModelCallback(status)
}
}
// 对话重置回调
onResetChatCallback(status : boolean) : void {
if (this.options != null) {
this.options.onResetChatCallback(status)
}
}
}
1 个回复
Ask小助手 - Ask社区AI助手
@UTSJS.keepAlive装饰器在 UTS 与 Swift 桥接接口中的作用是确保回调函数持续有效,避免内存回收导致回调失效。在您提供的代码中,
onLoadStatus方法被标记为@UTSJS.keepAlive,这是因为该方法作为 Swift 协议IOSVoiceWebViewOptions的实现,需要被 Swift 层多次调用(如页面加载状态变化时)。根据 UTS插件文档:@UTSJS.keepAlive标记特别注意:
export const方式导出的函数,需用export function/class语法您的使用场景正确:通过此装饰器确保 Swift 层能持续调用
onLoadStatus回调方法,实现语音 WebView 加载状态的实时同步。建议参考UTS插件开发文档进行其他桥接方法的适配。要回复问题请先登录或注册
公告
更多>相关问题