How to display a WebView in SwiftUI. I hope they come out with a WebView that doesn't have to use UIViewRepresentable as soon as possible.
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
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
NavigationView { | |
NavigationLink(destination: SecondView(urlString: "https://www.google.com/")) { | |
Text("Push Me") | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
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
import SwiftUI | |
import WebKit | |
struct MyWebView: UIViewRepresentable { | |
let urlString: String | |
func makeUIView(context: Context) -> WKWebView { | |
return WKWebView() | |
} | |
func updateUIView(_ uiView: WKWebView, context: Context) { | |
guard let url = URL(string: urlString) else { | |
return | |
} | |
uiView.load(URLRequest(url: url)) | |
} | |
} | |
struct MyWebView_Previews: PreviewProvider { | |
static var previews: some View { | |
MyWebView(urlString: "https://www.google.com/") | |
} | |
} |
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
import SwiftUI | |
struct SecondView: View { | |
let urlString: String | |
var body: some View { | |
MyWebView(urlString: urlString) | |
.navigationBarTitle(Text(urlString), displayMode: .inline) | |
} | |
} | |
struct SecondView_Previews: PreviewProvider { | |
static var previews: some View { | |
SecondView(urlString: "https://www.google.com/") | |
} | |
} |