Created
November 16, 2023 10:44
-
-
Save tosinonikute/75dd09ad21a4d39180649e3ed3a965c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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