Skip to content

Instantly share code, notes, and snippets.

@tosinonikute
Created November 16, 2023 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tosinonikute/75dd09ad21a4d39180649e3ed3a965c4 to your computer and use it in GitHub Desktop.
Save tosinonikute/75dd09ad21a4d39180649e3ed3a965c4 to your computer and use it in GitHub Desktop.
// 1 . WebViewContainer is a NSViewRepresentable SwiftUI view that wraps a WKWebView and loads a URL.
// 2. The ContentView contains the WebViewContainer and a title.
// 3. The WebViewApp is the main entry point for the app.
import SwiftUI
import WebKit
struct WebViewContainer: NSViewRepresentable {
let urlString: String
func makeNSView(context: Context) -> WKWebView {
let webView = WKWebView()
if let url = URL(string: urlString) {
let request = URLRequest(url: url)
webView.load(request)
}
return webView
}
func updateNSView(_ nsView: WKWebView, context: Context) {
// Update the view if needed
}
}
@main
struct WebViewApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
VStack {
Text("Web View App")
.font(.title)
WebViewContainer(urlString: "https://www.example.com")
.frame(width: 800, height: 600)
}
}
}
@available(macOS 12.0, *)
extension NSTextField {
open override var focusRingType: NSFocusRingType {
get { .none }
set { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment