Created
December 1, 2023 21:12
-
-
Save rmaafs/ca4d2d03990f323d7ac513436b9cd213 to your computer and use it in GitHub Desktop.
WebView without edges
This file contains hidden or 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 WebView: UIViewRepresentable { | |
let urlString: String = "http://localhost:3000" | |
class Coordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler { | |
var webView: WKWebView? | |
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | |
self.webView = webView | |
} | |
// Receive message | |
func userContentController( | |
_ userContentController: WKUserContentController, | |
didReceive message: WKScriptMessage | |
) { | |
print(message.body) | |
self.messageToWebview(msg: "Received at \(Date())") | |
} | |
func messageToWebview(msg: String) { | |
self.webView?.evaluateJavaScript("onAppMessage('\(msg)');") | |
} | |
} | |
func makeCoordinator() -> Coordinator { | |
return Coordinator() | |
} | |
func makeUIView(context: Context) -> WKWebView { | |
let coordinator = makeCoordinator() | |
let userContentController = WKUserContentController() | |
userContentController.add(coordinator, name: "bridge") | |
let configuration = WKWebViewConfiguration() | |
configuration.userContentController = userContentController | |
let _wkwebview = WKWebView(frame: .zero, configuration: configuration) | |
_wkwebview.navigationDelegate = coordinator | |
// Remove edges | |
_wkwebview.scrollView.contentInsetAdjustmentBehavior = .never | |
return _wkwebview | |
} | |
func updateUIView(_ webView: WKWebView, context: Context) { | |
if let url = URL(string: urlString) { | |
let request = URLRequest(url: url) | |
webView.load(request) | |
} | |
} | |
} | |
struct ContentView: View { | |
var body: some View { | |
VStack { | |
//Text("Web View Page") | |
// Remove edges | |
WebView().edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment