To open a website in SwiftUI, we can make use of WebKit and Safari. Here's an example code snippet using UIHostingController:
main.swift757 chars36 lines
In the above code, we have created a WebView
struct that conforms to the UIViewRepresentable
protocol. It is responsible for creating a WKWebView
instance and loading the specified URL.
Then in the ContentView
, we use the WebView
and embed it in a NavigationView
. Finally, we set the navigation bar title to show "Google".
This is just one way to open a website in SwiftUI using WebKit and Safari. There are other approaches as well, like using UIApplication.shared.open()
to launch Safari directly, but this is one of the most common ways to achieve this.
gistlibby LogSnag