I have doubt that how i can i create a webview using button click as same as javascript. I have searched alot but no result please if anyone have idea on that please send me simple code on creating webview in button click
Post not yet marked as solved
import SwiftUIimport WebKitimport UIKitstruct ContentView: View { @State var showMenu = false @State private var selection: Int? = nil @State var refresh: Bool = false var api = "https://google.com/" let endpoint = "" var body: some View { NavigationView { VStack { WebView(request: URLRequest(url: URL(string : self.api + self.endpoint)!)) }.navigationBarTitle(Text(""), displayMode: .inline) .navigationBarItems(leading: HStack{ Button(action:{}){ Image("test2") .foregroundColor(Color.black) --------------> so in this button i want to change web url .padding(.top, 5.0) .animation(.linear) }}, trailing: HStack{ Button(action:{}){ Image("test2") .foregroundColor(Color.black) }; Button(action:{}){ Image("test2") .foregroundColor(Color.black) }; NavigationLink(destination: testview()) { Text("") Image("test2") } } ) } } func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { if let url = navigationAction.request.url { if url.host == "www.apple.com" { UIApplication.shared.open(url) decisionHandler(.cancel) return } } ;func onAppear(){ UINavigationBar.appearance().barTintColor = UIColor(red: 0, green: 100/255, blue: 205/255, alpha: 1) UINavigationBar.appearance().tintColor = UIColor.white UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white] } }struct WebView : UIViewRepresentable { // static is only for demo, this can be some external model static var cache = [URL: WKWebView]() let request: URLRequest func makeUIView(context: Context) -> WKWebView { guard let url = request.url else { fatalError() } if let webView = WebView.cache[url] { return webView } let webView = WKWebView() WebView.cache[url] = webView return webView } func updateUIView(_ uiView: WKWebView, context: Context) { if uiView.url == nil { uiView.load(request) } }}
Post not yet marked as solved
Here i need help, i want to change webview by clicking a button in navigation bar item please help me, can be this possible... IN swiftuiImport Swiftuivar body: some View { NavigationView { VStack { WebKit(request: URLRequest(url: URL(string : api + endpoint)!)) }.navigationBarTitle(Text(""), displayMode: .inline) .navigationBarItems(leading: HStack{ Button(action:{}){ Image("test2") .foregroundColor(Color.black) }; };Button(action:{ }){ Image("test2") .foregroundColor(Color.black) .padding(.top, 5.0) }}, trailing: HStack{ Button(action:{}){ Image("test2") .foregroundColor(Color.black) }; Button(action:{}){ Image("test2") .foregroundColor(Color.black) }; Button(action:{}){ Image("test2") .foregroundColor(Color.black) } } ) } };func onAppear(){ UINavigationBar.appearance().barTintColor = UIColor(red: 0, green: 100/255, blue: 205/255, alpha: 1) UINavigationBar.appearance().tintColor = UIColor.white UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white] } }struct WebKit: UIViewRepresentable{ let request: URLRequest func makeUIView(context: UIViewRepresentableContext<WebKit>) -> WKWebView { return WKWebView() } func updateUIView(_ uiView: WKWebView, context: Context) { uiView.load(request) } }