gpt4 book ai didi

ios - 如何在 swift 中加载带有自定义 url 的 webview

转载 作者:行者123 更新时间:2023-11-28 13:30:45 25 4
gpt4 key购买 nike

我有一个函数提供一个字符串并请求 webview 加载它但是 webview 没有加载这是我的代码

class BrowserModel{

func requestToURL(_ search:String)->WKWebView{
if let url = URL(string: "https://google.com/search?q=\(search.encode)"){
let webview = WKWebView()
let request = URLRequest(url: url)
webview.load(request)
print("not nil")
return webview
}
return WKWebView()
}
}

我尝试使用此代码来请求我自己的 Searchbar 委托(delegate),但未加载 webview

 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

webview = myWebview.requestToURL(self.searchbar.text!)
print("search pressed")
}

最佳答案

正如我在评论中提到的,问题出在本地 web View 生命周期中,尝试使用 WKWebView 的扩展,这里是如何操作的示例

    extension WKWebView {

func loadURL(_ string: String) {
guard let url = URL(string: "https://google.com/search?q=\(string)") else { return }
load(URLRequest(url: url))
}

}

这是搜索功能

private func addObserver() {
searchBar.rx.text.orEmpty
.distinctUntilChanged()
.debounce(.seconds(1), scheduler: MainScheduler.instance)
.subscribe(onNext: { query in
self.webView.loadURL(query)
}, onError: { error in
print(error.localizedDescription)
})
.disposed(by: disposeBag)
}

在你的情况下你可以使用

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
webView.loadURL(searchbar.text ?? "")
}

关于ios - 如何在 swift 中加载带有自定义 url 的 webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57371416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com