FullStack
FullStack
  • 发布:2024-12-19 00:03
  • 更新:2024-12-19 00:03
  • 阅读:17

ios uts WKWebView 部分代理没有回调

分类:uts
import { UIButton, UIControl } from "UIKit"  
import { URL, URLRequest } from "Foundation"  
import { WKWebView, WKNavigation, WKNavigationAction, WKNavigationActionPolicy, WKWebViewConfiguration, WKWindowFeatures, WKUIDelegate, WKNavigationDelegate } from 'WebKit';  

export class NativeButton implements WKUIDelegate, WKNavigationDelegate {  

    element : UniNativeViewElement;  
    button : WKWebView | null;  

    constructor(element : UniNativeViewElement) {  
        // 接收组件传递过来的UniNativeViewElement  
        this.element = element;  
        super.init()  
        this.bindView();  
    }  

    // element 绑定原生view  
    bindView() {  
        // 初始化原生 UIButton  
        this.button = new WKWebView()  
        const url = new URL(string = "https://www.baidu.com/")!  
        const request : URLRequest = new URLRequest(url = url)  
        this.button?.load(request)  
        this.element.bindIOSView(this.button!);  
        (this.button! as WKWebView).navigationDelegate = this;  
        (this.button! as WKWebView).uiDelegate = this;  
    }  

    webView(webView : WKWebView, @argumentLabel("didFinish") navigation : WKNavigation) {  
        console.log('webViewDidFinishNavigation')  
    }  
    webView(webView : WKWebView, @argumentLabel("didStartProvisionalNavigation") navigation : WKNavigation) {  
        console.log('didStartProvisionalNavigation')  
        //webView.goBack();  
    }  
    webView(webView : WKWebView, @argumentLabel("didCommit") navigation : WKNavigation) {  
        console.log('didCommit')  
        //webView.goBack();  
    }  

    //    optional func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping @MainActor @Sendable (WKNavigationActionPolicy) -> Void)  
    webView(webView : WKWebView, @argumentLabel("decidePolicyFor") navigationAction : WKNavigationAction, @escaping @MainActor @Sendable decisionHandler : (WKNavigationActionPolicy : (WKNavigationActionPolicy)) => void) {  
        console.log('decidePolicyFor') //没有回调  
        decisionHandler(WKNavigationActionPolicy.cancel)  
    }  
    //    optional func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?  

    webView(webView : WKWebView, @argumentLabel("createWebViewWith") configuration : WKWebViewConfiguration, @argumentLabel("for") navigationAction : WKNavigationAction, windowFeatures : WKWindowFeatures) : WKWebView {  
        console.log('createWebViewWith') //没有回调  
        if (navigationAction.targetFrame == nil) {  
            webView.load(navigationAction.request)  
        }  
        return webView  
    }  

    updateText(text : string) {  
        // 更新 button 显示文字  
        //this.button?.setTitle(text, for = UIControl.State.normal)  
    }  

    /**  
     * 按钮点击回调方法  
     * 在 swift 中,所有target-action (例如按钮的点击事件,NotificationCenter 的通知事件等)对应的 action 函数前面都要使用 @objc 进行标记。  
     */  
    @objc buttonClickAction() {  
        //构建自定义 UniNativeViewEvent 对象  
        let event = new UniNativeViewEvent("customClick")  
        //触发自定义事件  
        this.element.dispatchEvent(event)  
    }  

    destroy() {  
        // 释放 UTS 实例对象,避免内存泄露  
        UTSiOS.destroyInstance(this)  
    }  
}
2024-12-19 00:03 负责人:无 分享
已邀请:

要回复问题请先登录注册