如何使WKWebView快速运行并用于macOS App [英] how do I get WKWebView to work in swift and for an macOS App

查看:774
本文介绍了如何使WKWebView快速运行并用于macOS App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在被裁定为重复项之前,事实并非如此. WKWebView上的每个问题几乎都是关于iOS Apps中的WKWebView,而不是macOS Apps,不同之处在于,仅实现了UIViewController接口而不是macOS中的NSViewController接口.

before this get's shot down for being a duplicate, it isn't. Pretty much every question on WKWebView here is about WKWebView in iOS Apps, not macOS Apps, with the difference being pretty much just the UIViewController interface being implemented instead of the NSViewController interface in macOS.

Apple文档中的示例代码以及可以在网上找到的Controller代码不起作用.尽管它确实可以毫无问题地进行编译,但Webview仍然保持不活动状态.

The example code in Apple's Documentation as well as the Controller code, that can be found online doesn't work. Altough it does compile without a problem the webview stays inactive.

WkWebView中是否存在我只是看不到的东西?或者是Bug?
我什至从教程中复制了一些代码,展示了如何在iOS上执行此操作,并且只是将UIViewController更改为NSViewController(因为这是唯一的区别),但没有用.

Is there something that I just didn't see or is this a bug in WKWebView ?
I even copied some code from tutorials showing how to do this for iOS and just changed UIViewController to NSViewController (since that was the ONLY difference), yet it didn't work.

ViewController.swift中的以下代码不起作用. 如果它是
class ViewController: NSViewController, WKUIDelegate

The following code in ViewController.swift does not work. It also doesn't work if it's
class ViewController: NSViewController, WKUIDelegate

import Cocoa;
import WebKit;
class ViewController: NSViewController {
    @IBOutlet weak var webView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let url=URL(string: "http://safetec-cam.biz/images/webcam_extern/bad-nauheim_bahnhof_west.jpg");
        webView.load(URLRequest(url: url!));
    }
}

如果通过将UIViewController交换为NSViewController来完成此操作,它也将不起作用 来自https://developer.apple.com/documentation/webkit/wkwebview 的图片

it also doesn't work if it's done like this with the UIViewController exchanged for NSViewController image from https://developer.apple.com/documentation/webkit/wkwebview

推荐答案

我建议您从头开始:

设置要加载的网址:

let myURLString = "https:yourWebLink"
let url = URL(string: myURLString)
let request = URLRequest(url: url!)

在Web视图中初始化并加载请求:

let webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request)

实施WKNavigationDelegate来跟踪页面加载/错误:

extension ViewController: WKNavigationDelegate {

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("Started to load")
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("Finished loading")
    }

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print(error.localizedDescription)
    }
}

有关更多参考,请检查: https://iosdevcenters.blogspot.com/2016/05/creating-simple-browser-with-wkwebview.html

For further reference check: https://iosdevcenters.blogspot.com/2016/05/creating-simple-browser-with-wkwebview.html

这篇关于如何使WKWebView快速运行并用于macOS App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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