菜鸟教程小白 发表于 2022-12-12 10:28:10

iOS:JavaScript 无法在 WKWebView 上运行


                                            <p><p>我使用 WKWebView 来显示一个网页。当我触摸页面上的按钮时,一些 javascript 代码应该可以工作并向我显示警报 View 。但它不起作用。如果我在 Safari 上打开该页面,它运行良好。这是我的代码</p>

<pre><code>WKWebViewConfiguration *configuration = ;
configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
configuration.preferences.javaScriptEnabled = YES;

_webView = [ initWithFrame:self.view.bounds configuration:configuration];
self.view = _webView;
NSURLRequest *req = ];
;
;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>WKWebView</code> 不会自动显示 JavaScript 警报。会通过通知
<code>webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:</code> 定义在 <code>WKUIDelegate</code> 上。您可以使用传入的信息通过 <code>UIAlertController</code> 显示 native 警报。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS:JavaScript 无法在 WKWebView 上运行,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35149251/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35149251/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS:JavaScript 无法在 WKWebView 上运行