Skip to content

Displaying a WebView in SwiftUI

   

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.

WebView

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()
}
}
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/")
}
}
view raw MyWebView.swift hosted with ❤ by GitHub
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/")
}
}

  1. Create a sequential numbered array in Swift
  2. Delete and add contents of List with SwiftUI
  3. Hide keyboard with buttons in SwiftUI
  4. Detect keyboard height and appearance with SwiftUI
  5. Input a multi-line string with SwiftUI
  6. Show picker from bottom with SwiftUI
  7. Using UIImagePickerController with SwiftUI