5

We are adding a feature to our Cordova app to catch unhandled exceptions and restart the app. We would like the browser history to be cleared in this case so the user can't hit back on an Android device to go back to the screen that crashed.

It isn't possible to clear the browser history programmatically, but I expected there to be a Cordova plugin that reinstantiates the whole app (i.e. deletes the current webview and creates a new one). I wasn't able to find anything like this though.

Is there a good way to do this that will work on both iOS and Android?

Matthew Gertner
  • 4,487
  • 2
  • 32
  • 54

2 Answers2

3

In Android for every WebView instance you can do:

webView.clearCache(true);
webView.clearHistory();
webView.clearFormData();

For IOS please read here: Clearing UIWebview cache

RonTLV
  • 2,376
  • 2
  • 24
  • 38
  • Unless I am missing something, this won't work on iOS since it doesn't clear the back/forward cache (aka history). – Matthew Gertner Aug 21 '18 at 11:36
  • I'm accepting this answer since it's the only one that addresses the browser history. Unfortunately there doesn't seem to be a straightforward way to clear the history on iOS but that isn't essential for our use case since there's no hardware back button on iOS anyway. – Matthew Gertner Aug 24 '18 at 14:32
1

I'd rather suggest cordova-plugin-cache-clear, in order to handle more than just Chrome on Android. but having an Activity restart itself cannot be accomplished by some Cordova plugin (and in case of an Exception, not even by Activity.recreate()), but it would require a helper Service, which gets notified (of course these have to be handled exceptions, un-handled exceptions would nevertheless just crash) and then handles the situation accordingly. It might make more sense, to iron out possible reasons for crashes - instead of wasting time to creating questionable workarounds for code which was not properly forged, in the first place. besides, if this is JavaScript which bugs out, there might be no way to work around these issues at all, but to fix them.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • This would be perfect but it doesn't remove the back/forward history. As far as I can see, there is no way to do this with a `UIWebView`. You simply need to create a new one, and I can't see a good way to do that the primary web view in a Cordova app. – Matthew Gertner Aug 21 '18 at 11:35
  • Android `WebView` https://stackoverflow.com/questions/8103532/how-to-clear-webview-history-in-android & iOS `WKWebView` https://stackoverflow.com/questions/46086691/how-to-clear-the-wkbackforwardlist-of-a-wkwebview ...writing such a Cordova plugin would be simple. I don't even understand why you would want to remove the history for a `WebView`... and what you mean by "crash"... JavaScript or the Cordova (WK) WebView? the `UIWebView` is even deprecated. – Martin Zeitler Aug 21 '18 at 12:57
  • My understanding is that Cordova still uses `UIWebView` (at least for the time being). To be specific, we are catching unhandled JavaScript exceptions and displaying a "fail whale" screen. We don't want Android users to be able to go back from there using the hardware back button. So actually we don't really need to worry about this on iOS since there is no way to go back anyway in a Cordova app. – Matthew Gertner Aug 21 '18 at 13:31
  • @MatthewGertner you should use `Jasmine` or `Karma` for testing ...because apps which crash suck in general (and tend to have low user retention), no matter how hard one tries to hide that... I've already wrote JS apps > 1MB code (where manual testing becomes impossible). with Cordova, one can also use the Chrome debug bridge, in order to directly debug on a hardware device. it's `chrome://inspect` ...crash reporting alike `Crashlytics` would make more sense (there one can also see, that the number of installs does not imply retention)... and one could even trigger it for JS errors (...) – Martin Zeitler Aug 21 '18 at 14:17
  • We have a very low number of unhandled JS exceptions but you can't catch everything during testing. We'd rather have an error page come up and reload the app than have it simply freeze (which is what happens at present). – Matthew Gertner Aug 21 '18 at 14:35
  • @MatthewGertner how can you know, without crash reporting ? reloading the `WebView` (and maybe triggering an error report, while doing so) would already be enough to reload the JS. – Martin Zeitler Aug 21 '18 at 14:45
  • We are reporting crashes to analytics as well. Just reloading is a bit abrupt so we display an error screen telling the user something went wrong. Anyway this question is really about how to remove the browser history on Android, not the error reporting UX. – Matthew Gertner Aug 21 '18 at 16:19