Created
February 12, 2020 15:36
-
-
Save joshbetz/2ff5922203240d4685d5bdb5ada79105 to your computer and use it in GitHub Desktop.
A simple 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 { | |
var body: some View { | |
Webview(url: URL(string: "https://google.com")!) | |
} | |
} | |
struct Webview: UIViewRepresentable { | |
let url: URL | |
func makeUIView(context: UIViewRepresentableContext<Webview>) -> WKWebView { | |
let webview = WKWebView() | |
let request = URLRequest(url: self.url, cachePolicy: .returnCacheDataElseLoad) | |
webview.load(request) | |
return webview | |
} | |
func updateUIView(_ webview: WKWebView, context: UIViewRepresentableContext<Webview>) { | |
let request = URLRequest(url: self.url, cachePolicy: .returnCacheDataElseLoad) | |
webview.load(request) | |
} | |
} |
Great way to display WebView in SwiftUI, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey man, this is great, thanks! How would you access the currently loaded URL?