【问题标题】:Swift WKWebView programmaticallySwift WKWebView 以编程方式
【发布时间】:2018-03-04 20:28:01
【问题描述】:

我想以编程方式生成具有特定大小的 WKWebView。

这段代码是我目前得到的:

override func viewWillAppear(_ animated: Bool) {
    let webview = WKWebView()
    webview.frame  = CGRect(width: UIScreen.main.bounds.width, height: 150) // not working
    webview.load(URLRequest(url: (forResource: "index", withExtension: "html", subdirectory: "myfolder")! as URL) as URLRequest)
    webview.uiDelegate = self
    self.view.addSubview(webview)
}


但它似乎不是那样工作的。

错误:“无法将'(forResource: String, withExtension: String, subdirectory: String)'类型的值强制转换为'URL'类型

任何帮助将不胜感激!

【问题讨论】:

  • 如果您能解释实际发生的事情与您预期会发生的事情,这将非常有帮助。
  • 请不要不必要地使用NSURLNSURLRequest。只需使用URLURLRequest
  • 将“加载”行拆分为大约三到四行,每行只做一件事,您会受益匪浅。

标签: ios iphone swift webview wkwebview


【解决方案1】:

Swift 5.如下图创建一个WKWebView类型的var

private let webView = WKWebView(frame: .zero)

在 viewDidLoad() 方法中添加 webView 到 View。

      webView.translatesAutoresizingMaskIntoConstraints = false
       self.view.addSubview(self.webView)
    // You can set constant space for Left, Right, Top and Bottom Anchors
                        NSLayoutConstraint.activate([
                            self.webView.leftAnchor.constraint(equalTo: self.view.leftAnchor),
                            self.webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
                            self.webView.rightAnchor.constraint(equalTo: self.view.rightAnchor),
                            self.webView.topAnchor.constraint(equalTo: self.view.topAnchor),
                            ])
        // For constant height use the below constraint and set your height constant and remove either top or bottom constraint
        //self.webView.heightAnchor.constraint(equalToConstant: 200.0),

        self.view.setNeedsLayout()
        var request = URLRequest(url: URL.init(string: "https://www.google.com"))
        self.webView.load(request)

【讨论】:

    【解决方案2】:

    我自己找到了解决方案:

    这段代码运行良好:

    let webview = WKWebView()
        webview.frame  = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100)
        webview.load(URLRequest(url: Bundle.main.url(forResource: "index", withExtension:"html", subdirectory: "subdirectories")! as URL) as URLRequest)
        self.view.addSubview(webview)
    

    --

    这是我遗漏的那一行:

    url: Bundle.main.url(forResource: "index", withExtension:"html", subdirectory: "subdirectories")! as URL)
    

    【讨论】:

      【解决方案3】:
          let webConfiguration = WKWebViewConfiguration()
          let startScript = Bundle(for: Background.self).url(forResource: "my_js_script", withExtension: "js")!
          let scripts =         do {
              return try String(contentsOf: startScript, encoding: .utf8)
          }
          catch {
              let message = "Could not load file at: \(startScript)"
              fatalError(message)
          }
          let script = WKUserScript(source: scripts, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: true)
      
          let contentController: WKUserContentController = WKUserContentController()
          contentController.addUserScript(script)
      
          contentController.add(self, name: "backgroundListener")
          webConfiguration.userContentController = contentController
      
          self._webView = WKWebView(frame: .zero, configuration: webConfiguration)
          self._webView!.customUserAgent = "my-Bridge"
          let html : String = """
                               <html>
                                 <head></head>
                                 <body></body>
                               </html>
                              """;
          self._webView!.loadHTMLString(html, baseURL: nil)
      

      【讨论】:

      • 您能否在此答案中添加一些描述,以便为该编码解决方案添加一些上下文?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      相关资源
      最近更新 更多