在Web视图中加载URL的MacOS问题

时间:2018-03-07 11:26:50

标签: swift xcode macos webview

我已经完成了以下代码,但我在使用在运行时显示错误的网址加载webview时出现问题

  

线程1:信号SIGABRT

所以请帮助我,我刚刚开始macOS应用程序开发,

谢谢,

import Cocoa

import WebKit


class ViewController: NSViewController, WebFrameLoadDelegate {


    @IBOutlet weak var webView: WebView!

    let urlpath = NSURL(string: "http://www.google.com/")!


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //let url = "https:www.apple.com"
        //self.webView.mainFrame.load(NSURLRequest(URL: NSURL(string: url)! as URL))

        webView.policyDelegate = self as! WebPolicyDelegate

        webView.mainFrame.load(NSURLRequest(url: self.urlpath as URL ) as URLRequest!)

    }


    override var representedObject: Any? {

        didSet {

            // Update the view, if already loaded.

        }

    }




}

1 个答案:

答案 0 :(得分:1)

您没有实现WebPolicyDelegate

webView.policyDelegate = self as! WebPolicyDelegate

会失败。

self(ViewController)没有实现WebPolicyDelegate。

你需要这样做:

class ViewController: NSViewController, WebFrameLoadDelegate, WebPolicyDelegate {

并实现委托。

还要确保您的webView

@IBOutlet weak var webView: WebView!)

已连接到Interface Builder中的webView!