iOS Swift SFSafariViewController 更新新 URL 并刷新视图

iOS Swift SFSafariViewController update new URL and refresh view

我正在使用 SFSafariViewController 加载网络链接,
在我的例子中,首先我需要在 process/time(比如 10 秒)之后打开一个 URL 我需要在同一个选项卡中更新我的 URL 并刷新 SFSafariViewController。

if let url = URL(string: "Google.com") {
    let VC= SFSafariViewController(url: url)
    VC.delegate = self
    self.present(VC, animated: true, completion: nil)
}

There is any way to update The URL and refresh the current SFSafariViewController page?

SFSafariViewController 专门用于呈现您的应用程序无法与之交互的 Web 内容。来自文档:

The user's activity and interaction with SFSafariViewController are not visible to your app, which cannot access AutoFill data, browsing history, or website data.

与您的问题更具体相关的是,SFSafariViewController 的文档还说:

If your app customizes, interacts with, or controls the display of web content, use the WKWebView class.

既然你想控制网页内容的显示(改变 URL)你应该使用 WKWebView。

使用WKWebView解决问题。

class webViewController: WKNavigationDelegate, WKUIDelegate {
    var webview: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        webview = WKWebView(frame: self.view.bounds)
        self.view.addSubview(webview)
        webview.navigationDelegate = self
        webview.uiDelegate = self
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if let url = navigationAction.request.url {
            // Dosomthing()
            decisionHandler(.cancel)
            return
        }
        decisionHandler(.allow)
    }
    
    func Dosomthing() {
        self.webview.load(URLRequest(url: newURL))
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            self.webview.reload()
        }
    }
}