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()
}

如果空白

那就是权限没有开

相关推荐
Architect_Lee10 小时前
mac快速安装mysql
数据库·mysql·macos
用户03321266636710 小时前
使用 Python 将 HTML 内容添加到 PowerPoint 演示文稿中
python·html
User_芊芊君子11 小时前
STT + LLM + TTS:用一条流水线搓了个中英双向翻译助手
ide·macos·xcode
东坡肘子12 小时前
Apple Intelligence 已通过审核,即将在中国提供服务 -- 肘子的 Swift 周报 #148
人工智能·swiftui·swift
eric-sjq1 天前
10 分钟实战:用 0.6B 本地模型生成中文网页(WanlyFrontend + 编译器全流程)
javascript·机器学习·自然语言处理·html
凤山老林1 天前
在 Apple 芯片的 Mac 上通过 VMware Fusion 使用 Windows 11
windows·macos·vmware·虚拟机
c&0xff001 天前
mac通过网线连接树莓派
macos
孙启超1 天前
Token太贵自己写了一个mac版开源AI编程工具
人工智能·macos·开源·llm·agent·ai编程·ai应用开发
2601_965798471 天前
Launch Your Wellness App Fast: The Truth About Meditation Source Code
前端·macos·ios·objective-c·cocoa
iReachers2 天前
HTML打包APK实现拍照、录像、录音和文件选择教程
html·安卓·相机·apk打包·麦克风