Last active
October 6, 2021 03:15
-
-
Save eunice730711/636c13c03dac5a5461ebeae7bf64890c to your computer and use it in GitHub Desktop.
SwiftUI: WebView
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 ContentView: View { | |
@State var urlString = "https://google.com" | |
var body: some View { | |
SwiftUIWebView(urlString: $urlString) | |
} | |
} | |
struct SwiftUIWebView: UIViewRepresentable { | |
@Binding var urlString: String | |
func updateUIView(_ uiView: WKWebView, context: Context) { | |
guard let webURL = URL(string: urlString) else { | |
return | |
} | |
let request = URLRequest(url: webURL) | |
uiView.load(request) | |
} | |
func makeUIView(context: Context) -> WKWebView { | |
return WKWebView(frame: .zero) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment