Xamarin iOS WKWebView显示JavaScript警报

Xamarin iOS WKWebView showing JavaScript Alert

本文关键字:JavaScript 警报 显示 WKWebView iOS Xamarin      更新时间:2023-09-26

我想在我的WKWebView中显示JavaScript警报。我已经实现了IWKUIDelegate及其方法RunJavaScriptAlertPanel、RunJavaScriptConfirmPanel。这些会被调用作为JavaScript警报。我想知道如何处理这些按钮以及对这些按钮的操作。

有没有一种方法可以为所有JavaScript警报编写一个通用的UIAlertView。例如,下面我添加了errorAlert.AddButton("OK");但实际上我不知道按钮的数量和按钮的标题。还有如何处理completionHandler操作

    [Export ("webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:")]
    public void RunJavaScriptConfirmPanel (WKWebView webView, String message, WKFrameInfo frame,  Action<bool> completionHandler)
    {
        Console.WriteLine ("RunJavaScriptConfirmPanel");

        UIAlertView errorAlert = new UIAlertView();
        errorAlert.Title = "Alert";
        errorAlert.Message = message;
        errorAlert.AddButton("OK");
        errorAlert.Show();
    }

我不确定你要在哪里处理这个操作,无论是在javascript中还是在应用程序中使用C#,所以我会尝试解决这两个问题。简单的答案是,让javascript处理它,如果需要,在对话框中进行选择后,从javascript回调应用程序。

警报示例

    public override void RunJavaScriptAlertPanel(WKWebView webView, string message, WKFrameInfo frame, Action completionHandler)
    {
        var alert = UIAlertController.Create("Alert", message, UIAlertControllerStyle.Alert);
        alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
        //Let javascript handle the OK click by passing the completionHandler to the controller
        ViewController.PresentViewController(alert, true, completionHandler);
    }

确认示例

    public override void RunJavaScriptConfirmPanel(WKWebView webView, string message, WKFrameInfo frame, Action<bool> completionHandler)
    {
        var alert = UIAlertController.Create("Please confirm", message, UIAlertControllerStyle.Alert);
        //Let javascript handle the OK click by passing the completionHandler to the controller
        alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, action => completionHandler(true)));
        //Let javascript handle the Cancel click by passing the completionHandler to the controller
        alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, action => completionHandler(false)));
        ViewController.PresentViewController(alert, true, null);
    }

提示示例

    public override void RunJavaScriptTextInputPanel(WKWebView webView, string prompt, string defaultText, WKFrameInfo frame, Action<string> completionHandler)
    {
        var alert = UIAlertController.Create("Prompt", prompt, UIAlertControllerStyle.Alert);
        alert.AddTextField(textfield => { textfield.Placeholder = defaultText; });
        alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, action => completionHandler(alert.TextFields[0].Text)));
        alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, action => completionHandler(null)));
        ViewController.PresentViewController(alert, true, null);
    }

因此,这为您提供了一个如何将按钮单击传递回javascript的示例。如果您希望javascript进行回调,则需要通过从WKScriptMessageHandler进行子类化来注册脚本处理程序。这里有一个基本的例子https://forums.xamarin.com/discussion/41777/wkwebview-caching-not-working

值得注意的是,UIAlertView已被弃用。您可能需要开始使用UIAlertController。看见https://stackoverflow.com/a/32690371/5606840.如果您希望访问视图控制器以显示警报,请在初始化时将其传递到WKUIDelegate类中。

编辑

扩展如何在javascript 中处理结果

alert('This is an alert');

web视图将暂停javascript线程并调用RunJavaScriptAlertPanel。单击OK按钮后,它将在web视图中回调completionHandler,并执行其余的javascript代码。

if (confirm('Do you want to confirm this?')) {
    //Do some code here when the user clicks ok
} else {
    //Do other code here when the user clicks cancel
}

与警报示例一样,当javascript遇到confirm方法时,它将调用RunJavaScriptConfirmPanel。然后,您可以显示带有通过消息传递的文本的对话框,以及"确定"answers"取消"按钮。根据单击的按钮,该方法将把true或false推回到web视图,并允许通过完成处理程序执行其余的javascript代码。