【问题标题】:WKWebView does not trigger method createWebViewWithConfigurationWKWebView 不触发方法 createWebViewWithConfiguration
【发布时间】:2020-03-25 15:04:02
【问题描述】:

我正在尝试在 HTML 源代码中运行 Javascript。里面的代码假设用我给出的另一个 HTML 源代码打开一个新窗口。我在这里有什么错误?

(我的目标是证明WKWebView有能力打开嵌套的弹窗)也就是说,Webview打开了一个PopUpWindow A,那么PopUpWindow A就会window.open()PopUpWindow B,然后 PopUpWindow B 将window.open() PopUpWindow C。

在我的 WKWebView 中,我做了以下事情:

  1. 已实现WKUIDelegate
  2. 设置_webView.UIDelegate = self;
  3. 设置两个首选项: wkWebViewConfig.preferences.javaScriptCanOpenWindowsAutomatically = YES; wkWebViewConfig.preferences.javaScriptEnabled = YES;
  4. 在下面创建方法
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
  if (!navigationAction.targetFrame.isMainFrame) {
    [webView loadRequest:navigationAction.request];
  }
  return nil;
}

我的示例 html 源代码如下:

<!DOCTYPE html>
<html>
  <head>
     <script language="javascript">
      function init()
      { 
          setTimeout( () => {
            window.open("testing2.html","mywindow","toolbar=no,menubar=no,location=no,directories=no,width=910,height=750");
          }, 3000)
          document.redirectForm.target="mywindow";
          document.redirectForm.submit();
      }
    </script>
  </head>
  <body>
    Going to testing2
    <form>
         <input type="button" onclick="init()" value="Click" />
      </form>
      <script type="text/javascript">
        init();
      </script>
  </body>
</html>

我尝试用https://www.google.com 替换"testing2.html",它确实显示在Google 网站上。但同样,我的目标是确保我的 WKWebView 能够打开 Nested PopUp Window 由于某些客户端 API 的一些架构设计

我读过的一些类似的问题和答案:

  1. https://stackoverflow.com/a/33198751/4311268

  2. https://stackoverflow.com/a/39073499/4311268

  3. https://stackoverflow.com/a/25853806/4311268

【问题讨论】:

    标签: ios objective-c wkwebview wkuidelegate


    【解决方案1】:

    我认为原因是window.open 没有打开本地文件(例如,testing.html)。因此我安装了http-server 来在本地主机上托管我的文件。

    1. npm i http-server -g
    2. cd 到我放置所有 html 文件的文件夹
    3. 运行http-server
    4. 运行最后一个IP地址:8080从http-server返回
    5. 您将在 localhost 中看到所有 .html 文件
    6. 将 .html 文件替换为 localhost 链接

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 2011-05-28
      • 2012-11-07
      相关资源
      最近更新 更多