swiftui macOS实现加载本地html文件

swift 复制代码
import SwiftUI
import WebKit

struct ContentView: View {
    var body: some View {
        VStack {
            Text("测试")
            
            HTMLView(htmlFileName: "localfile") // 假设你的本地 HTML 文件名为 index.html
                        .frame(minWidth: 100, minHeight: 100) // 设置 HTMLView 的最小尺寸
        }
        .frame(minWidth: 200, minHeight: 200) // 设置 ContentView 的最小尺寸
    }
}

struct HTMLView: NSViewRepresentable {
    let htmlFileName: String

    func makeNSView(context: Context) -> WKWebView {
        let webView = WKWebView()
        return webView
    }

    func updateNSView(_ nsView: WKWebView, context: Context) {
        if let htmlPath = Bundle.main.url(forResource: htmlFileName, withExtension: "html") {
            let request = URLRequest(url: htmlPath)
            nsView.load(request)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

#Preview {
    ContentView()
}

如果空白

那就是权限没有开

相关推荐
nbwenren35 分钟前
2026实测:Gemini 3 镜像站视觉能力实践——拍照原型图,一键生成 HTML+CSS 代码
前端·css·html
爱上好庆祝7 小时前
学习js的第五天
前端·css·学习·html·css3·js
生而为虫7 小时前
Claude Code 最新版安装教程(Windows/Mac/Linux 全平台) 面向普通用户的 Claude Code 安装与模型接入指南
linux·windows·macos
前端老石人8 小时前
前端开发中的 URL 完全指南
开发语言·前端·javascript·css·html
jingqingdai310 小时前
别用正则格式化 HTML!我用 DOM 遍历实现零风险本地格式化,老项目重构效率直接拉满
前端·重构·html
李老师的Java笔记10 小时前
如何解决Mac升级完nodejs没有生效的问题?
macos
开开心心_Every10 小时前
轻量级PDF阅读器,仅几M大小打开秒开
linux·运维·服务器·安全·macos·pdf·phpstorm
Chengbei1110 小时前
轻量化 Web 安全日志分析神器 星川智盾日志威胁检测、地理溯源、MITRE ATT&CK 映射,支持 Windows/macOS/Linux
前端·人工智能·安全·web安全·macos·系统安全·安全架构
生而为虫11 小时前
在VScode中使用Claude Code agent并配置模型(仅mac电脑实际操作,windows电脑未实际操作如有问题可留言)
windows·vscode·macos
a11177611 小时前
“像风之翼“无人机巡检平台仪表盘
前端·javascript·开源·html·无人机