简体   繁体   English

WKWebView 不会加载(NSViewController,OS X)

[英]WKWebView Won't Load (NSViewController, OS X)

I am trying to build a simple WKWebView application that will display Google in the WebView.我正在尝试构建一个简单的 WKWebView 应用程序,它将在 WebView 中显示 Google。 The app works perfectly fine;该应用程序运行良好; however, upon implementing the WKWebView, the app no longer shows a window.然而,在实施 WKWebView 后,应用程序不再显示窗口。 It just simply launches the app (in the dock) with no NSView in sight.它只是简单地启动应用程序(在 dock 中),看不到 NSView。

ViewController.swift视图控制器.swift

import Cocoa
import WebKit

class ViewController: NSViewController {

    var webView: WKWebView?

    override func loadView() {
        self.webView = WKWebView()
        self.view = self.webView!

        let url = NSURL(string:"http://www.google.com/")
        let req = NSURLRequest(URL: url!)
        self.webView!.loadRequest(req)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

There doesn't seem to be any further examples online, nor does there seem to be any projects on GitHub.网上好像没有更多的例子,GitHub 上好像也没有什么项目。

Thanks.谢谢。 I haven't come across this before.我以前没有遇到过这个。

I guess there're some limitations behind Xcode Settings those hidden settings make your WKWebView NOT WORK!!! 我想Xcode设置背后有一些限制,那些隐藏的设置会使您的WKWebView无法正常运行!!!

You could try these ways, Maybe you will find the solution: 您可以尝试以下方式,也许您会找到解决方案:

1) Search these key words by Google, You'll find some tutorials: 1)通过Google搜索这些关键词,您将找到一些教程:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

2) Check your Project Setting panel. 2)检查项目设置面板。 Make sure you've CHECKED the NetWork [Incoming] & [OutComing] item: 确保已检查NetWork [Incoming]和[OutComing]项目:

屏幕截图

With Xcode 14 I needed the 'outgoing connection' and additionally the frame:对于 Xcode 14,我需要“传出连接”以及框架:

var webView: WKWebView!

override func loadView() {
    // these two lines are important:
    super.loadView()
    webView = WKWebView(frame:self.view.frame)

    view = webView
    
    let url = URL(string:"https://www.example.com/")!
    let req = URLRequest(url: url)
    webView.load(req)
    
    // webView.loadHTMLString("<html><body bgcolor=red><p>Hello, World!</p></body></html>", baseURL: nil)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM