WKWebView 不触发方法 createWebViewWithConfiguration [英] WKWebView does not trigger method createWebViewWithConfiguration

查看:17
本文介绍了WKWebView 不触发方法 createWebViewWithConfiguration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to run Javascript within in a HTML Source. The code inside is suppose to open a new window with another HTML Source I given. What's my mistake here?

(我的目标是证明WKWebView有能力打开嵌套弹出窗口)意思是,Webview打开了一个PopUpWindow A,然后PopUpWindow A会window.open() PopUpWindow B,然后PopUpWindow B 将window.open() PopUpWindow C.

(My goal is to prove the WKWebView has ability in opening nested popup window) Meaning that, Webview opened a PopUpWindow A, then PopUpWindow A will window.open() PopUpWindow B, then PopUpWindow B will window.open() PopUpWindow C.

在我的 WKWebView 中,我完成了以下操作:

In my WKWebView I have done the following:

  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 由于某些客户端 API 的某些架构设计而能够打开嵌套弹出窗口

I tried replacing "testing2.html" with https://www.google.com, it does show up Google website. But again, my goal here is to ensure my WKWebView is able to open Nested PopUp Window due to some architecture design of some client API

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

Some similar ques and ans I've read:

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

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

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

推荐答案

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

I think the reason is because window.open does not open local file (eg, testing.html). Hence I have installed http-server to host my files on localhost.

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

这篇关于WKWebView 不触发方法 createWebViewWithConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆