import SwiftUI
import WebKit
import UIKit
struct 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)
}
}
}