-2

For example, when I load a url inside the WebView in an iOS app, and when user clicked back button and navigates in the other sections of the app and when user returns back to the WebView section of the app, the WebView section should not be reloaded. Not only that, even if the user goes to other applications in the smartphone and returns back to the WebView section of the app, the WebView section should not be reloaded. How is it possible to preserve the state of the WebView in iOS?

Any possible solutions are welcomed!

rana
  • 13
  • 7
  • The way you phrased this question made me think of three different things, it was not clear at all. Instead of worrying about the webview being "reloaded", the more direct way to say this is that you would like the user to not be able to navigate away from the first page you loaded in the webview. – DrewG23 Jan 14 '20 at 14:12
  • Please mark my answer as the answer if it helped you. – DrewG23 Jan 15 '20 at 00:26

3 Answers3

0

You could possibly use UserDefaults to help with this. Something like the following untested code:

let defaultURLString = "https://yoursite.com/home"

override func viewDidLoad() {
    super.viewDidLoad()

    if let savedURL = UserDefaults.standard.url(forKey: "webViewUrl") {
        // Load webview with url here
        webView.load(URLRequest(url: savedURL))
    } else {
        // Load default url here
        if let defaultURL = URL(string: self.defaultURLString) {
            webView.load(URLRequest(url: defaultURL))
        }
    }
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    if let currentUrl = webView.url {
        UserDefaults.standard.set(currentUrl, forKey: "webViewUrl")
    }
}

You may need to clear the defaults for the webViewUrl key upon app startup.

DrewG23
  • 427
  • 3
  • 11
  • hi im slightly confused with ur implementation, whats the difference between the load webview url and default url?? – rana Jan 14 '20 at 04:02
  • Default url is your starting point, like `"https://yourwebsite.com/home"` or whatever url you want your user to first see. If the user navigates away from your default url like `"https://yourwebsite.com/about"`, we save that url and when the user returns we reload the webview to use the newly saved url, instead of the default url. – DrewG23 Jan 14 '20 at 04:10
  • thanks for ur answer. But i think u misunderstood my question. Based on ur example if the user starts with the ```"https://yourwebsite.com/home"``` and when user returns i want user to return back to ```"https://yourwebsite.com/home"``` and i dont want the url to be reloaded. Is there any solutions for this ?? – rana Jan 14 '20 at 04:18
  • Please try my other answer. – DrewG23 Jan 14 '20 at 04:21
0

If you would like the user to go to the same url every time they return to the webView try:

let defaultURLString = "https://yoursite.com/home"

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let defaultURL = URL(string: self.defaultURLString) {
        webView.load(URLRequest(url: defaultURL))
    }
}
DrewG23
  • 427
  • 3
  • 11
  • but does this way prevent the WKWebview from being reloaded? – rana Jan 14 '20 at 04:24
  • No it will be reloaded every time they visit the screen with this solution. Are you trying to explain that you do not want your users to navigate away from the default url like "https://yoursite.com/home" for example? Like if "https://yoursite.com/home" has links, then when the user clicks on a link, it should not work. Is that what you are wanting? – DrewG23 Jan 14 '20 at 04:29
  • yes i dont want it to be reloaded this is because currently the url im redirecting to is a smartchat bot. For instance, i want the user conversation with the chatbot to be there every time the user moves to another page. And when the user returns back to the url which contains the chatbot, i want the conversation to be there – rana Jan 14 '20 at 05:51
0

If you would like the user to not be able to navigate to other links within the webView, set the delegate to self and add the delegate method shouldStartLoadWith. Here you can check if the user clicked a link and prevent them from going anywhere, like so:

class TestViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet private(set) var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.webView.delegate = self

        let url = URL(string: "https://yoursite.com/home")
        self.webView.loadRequest(URLRequest(url: url!))
    }

    // MARK: UIWebViewDelegate

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
        if navigationType == .linkClicked {
            return false
        }
        return true
    }
}
DrewG23
  • 427
  • 3
  • 11