最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】iOS中UIWebView中如何改变URL并刷新页面

iOS crifan 6022浏览 0评论

iOS的swift项目中,需要改变UIWebView的url,并刷新页面,显示新内容

此处,稍等特殊一点是:

期间页面的加载,是混杂着和H5页面的交互的,

所以初始页面的最终加载,不是:

        if let url = NSURL(string: self.url) {

            let request = NSURLRequest(url: url as URL)

            self.webView.loadRequest(request as URLRequest)

        }

而是:

    func webViewDidFinishLoad(_ webView: UIWebView) {

        dispatchMain_async ({

            self.clearAllNotice()

        })

        self.context = webView.value(forKeyPath: “documentView.webView.mainFrame.javaScriptContext”) as? JSContext

    }

    

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool{

        self.jsContext = webView.value(forKeyPath: “documentView.webView.mainFrame.javaScriptContext”) as? JSContext

        let model = AppHost(currentVC: self)

        model.jsContext = self.jsContext

        

        // 这一步是将SwiftJavaScriptModel模型注入到JS中,在JS就可以通过WebViewJavascriptBridge调用我们暴露的方法了。

        self.jsContext?.setObject(model, forKeyedSubscript: “AppHost” as (NSCopying & NSObjectProtocol)!)

        

        // 注册到网络Html页面 请设置允许Http请求

        if let url = NSURL(string: self.url) {

            //WebView当前访问页面的链接 可动态注册

            _ = self.jsContext?.evaluateScript(try? String(contentsOf: url as URL, encoding: String.Encoding.utf8))

        }

        

        return true

    }

中的:

self.jsContext?.evaluateScript(try? String(contentsOf: url as URL, encoding: String.Encoding.utf8))

而此处是在另外的函数中:

    func getSelectReportFilter(selectReportFilter:ReportFilter) {

        dispatchMain_async ({

…..

        })

}

 去获得UIWebView的示例,并更换url,刷新页面,重新加载

ios swift UIWebView change url refresh

ios – Reloading a WebView with a new url from another ViewController – Stack Overflow

好像就是直接用:webView.loadRequest?

但是不知道是否适用于此处,刷新后,能继续支持原生和H5的交互吗?

ios – UIWebView won’t reload a new URL in Swift – Stack Overflow

ios – UIWebView doesn’t refresh – Stack Overflow

button – Swift: How to let a webview reload its url – Stack Overflow

ios – Unable to reload UIWebView to a new URL via code – Stack Overflow

ios – How do I use UIWebview pull to refresh in swift? – Stack Overflow

ios swift UIWebView new url

ios swift UIWebView load new url

ios swift UIWebView change to new url

ios8 – How to load URL in UIWebView in Swift? – Stack Overflow

ios – Swift 3 WebView – Stack Overflow

期间需要:

【已解决】iOS的Swift中如何编码url的query string

【总结】

此处更换url,重新加载新页面,就是普通的加载页面:

        if let url = NSURL(string: self.url) {

            let request = NSURLRequest(url: url as URL)

            self.webView.loadRequest(request as URLRequest)

        }

转载请注明:在路上 » 【已解决】iOS中UIWebView中如何改变URL并刷新页面

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.191 seconds, using 22.06MB memory