其他分享
首页 > 其他分享> > SwiftUI search web content All In One

SwiftUI search web content All In One

作者:互联网

SwiftUI search web content All In One

WebView


class WebViewManager : ObservableObject {
    var webview: WKWebView = WKWebView()
    
    init() {
        webview.load(URLRequest(url: URL(string: "https://apple.com")!))
    }
    
    func searchFor(searchText: String) {
        if let searchTextNormalized = searchText.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
           let url = URL(string: "https://google.com/search?q=\(searchTextNormalized)") {                        self.loadRequest(request: URLRequest(url: url))
        }
    }
    
    func loadRequest(request: URLRequest) {
        webview.load(request)
    }
    
    func goBack(){
        webview.goBack()
    }
    
    func goForward(){
        webview.goForward()
    }
    
    func refresh(){
        webview.reload()
    }
}

struct Webview : UIViewRepresentable {
    var manager : WebViewManager
    
    init(manager: WebViewManager) {
        self.manager = manager
    }
    
    func makeUIView(context: Context) -> WKWebView  {
        return manager.webview
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        
    }
}


struct ContentView: View {
    @StateObject private var manager = WebViewManager()
    @State private var searchText = ""
    @State private var txt = ""
    
    var body: some View {
        ZStack {
            HStack {
                TextField("Search", text: $searchText,onCommit: {
                    print(searchText)
                    manager.searchFor(searchText: searchText)
                })
                .keyboardType(.URL)
                .frame(width: UIScreen.main.bounds.size.width * 0.75 )
            }
        }
        Webview(manager: manager)
            .toolbar {
                ToolbarItemGroup(placement: .bottomBar) {
                    
                    Button(action: {
                        manager.goBack()
                    }) {
                        Image(systemName: "arrow.left")
                    }
                    Spacer()
                    
                    Button(action: {
                        manager.goForward()
                        
                    }) {
                        Image(systemName: "arrow.right")
                    }
                    
                    Spacer()
                    
                    Button(action: {
                        manager.refresh()
                    }) {
                        Image(systemName: "arrow.clockwise")
                    }
                }
            }
    }
}

https://stackoverflow.com/questions/67190890/how-to-search-a-custom-text-from-textfield-in-webkit-swiftui

demo

https://cdn.xgqfrms.xyz/

refs

SwiftUI WKWebView render iframe All In One

https://www.cnblogs.com/xgqfrms/p/13888753.html

WebView & WKWebView & UIWebView

https://www.cnblogs.com/xgqfrms/p/16284528.html


Flag Counter

©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载

标签:web,search,searchText,content,manager,func,webview,com,WKWebView
来源: https://www.cnblogs.com/xgqfrms/p/16286172.html