xcode 如何让 WKWebView 快速工作并适用于 macOS 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44465974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do I get WKWebView to work in swift and for an macOS App
提问by Ozymandias42
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.
在这个 get 因重复而被击落之前,它不是。这里几乎所有关于 WKWebView 的问题都是关于 iOS 应用程序中的 WKWebView,而不是 macOS 应用程序,区别几乎只是实现了 UIViewController 接口而不是 macOS 中的 NSViewController 接口。
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.
可以在网上找到的 Apple 文档中的示例代码以及控制器代码不起作用。尽管它确实可以毫无问题地编译,但 webview 仍然处于非活动状态。
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.
有什么我没有看到的东西还是 WKWebView 中的错误?
我什至从展示如何为 iOS 执行此操作的教程中复制了一些代码,并将 UIViewController 更改为 NSViewController(因为这是唯一的区别),但它不起作用。
The following code in ViewController.swift does not work.
It also doesn't work if it's class ViewController: NSViewController, WKUIDelegate
ViewController.swift 中的以下代码不起作用。如果它也不起作用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!));
}
}
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
如果使用 UIViewController 交换来自 https://developer.apple.com/documentation/webkit/wkwebview 的NSViewController图像,它也不起作用
回答by Jeba Moses
I recommend you to start from scratch:
我建议你从头开始:
Set Your URL to be loaded:
设置要加载的 URL:
let myURLString = "https:yourWebLink"
let url = URL(string: myURLString)
let request = URLRequest(url: url!)
Init and load request in webview:
在 webview 中初始化和加载请求:
let webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request)
Implement WKNavigationDelegate to trace your page Load/Error:
实施 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)
}
}
For further reference check: https://iosdevcenters.blogspot.com/2016/05/creating-simple-browser-with-wkwebview.html
如需进一步参考检查:https: //iosdevcenters.blogspot.com/2016/05/creating-simple-browser-with-wkwebview.html