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

问题描述 投票:0回答:1

我已经完成了下面的代码,但是我在使用url加载webview时遇到问题,该url在运行时显示错误

线程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.

        }

    }




}
swift xcode macos webview
1个回答
1
投票

您没有实现WebPolicyDelegate

webView.policyDelegate = self as! WebPolicyDelegate

将失败。

self(ViewController)不实现WebPolicyDelegate。

你需要这样做:

class ViewController: NSViewController, WebFrameLoadDelegate, WebPolicyDelegate {

并实现委托。

还要确保你的webView

@IBOutlet weak var webView: WebView!)

在Interface Builder中连接到您的webView!

© www.soinside.com 2019 - 2024. All rights reserved.