【问题标题】:Swift 5.3 macOS - Load Local HTMLSwift 5.3 macOS - 加载本地 HTML
【发布时间】:2021-06-10 18:39:07
【问题描述】:

我正在尝试将基于 html 的本地网站加载到我的 macOS 应用程序中,但我遇到了问题,我在 iOS 中可以使用,但在 macOS 中无法使用。

我的 ViewController.swift 看起来像这样

import Cocoa
import WebKit

class ViewController: NSViewController, WKUIDelegate, WKNavigationDelegate  {

    var webView: WKWebView!
    
    override func loadView() {
        let webConfig = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfig);
        webView.uiDelegate = self
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let htmlURL = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "website")!
        webView.loadFileURL(htmlURL, allowingReadAccessTo: htmlURL.deletingLastPathComponent())
    }

}

应用程序运行,但随后我在 Xcode 的控制台中得到此输出:

WebPageProxy::tryReloadAfterProcessTermination: 进程崩溃并且客户端没有处理它,没有重新加载页面,因为我们达到了最大尝试次数

我正在尝试做的事情的文档似乎缺乏我想知道是否有人可以帮我解决这个问题。

【问题讨论】:

  • 叫 uiDelegate 的家伙是什么来的?

标签: swift macos


【解决方案1】:

经过更多挖掘后,我需要做两件事:

  1. 修改我的viewDidLoad()代码:
let htmlURL = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "polarix-website")!
webView.loadFileURL(htmlURL, allowingReadAccessTo: htmlURL.deletingLastPathComponent())
view = webView
  1. 在签名和功能中启用Outgoing Connections (Client)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2023-03-11
    • 2018-12-27
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多