Search code examples
safariios13

detecting WebSQL support (or lack thereof) in Safari 13


Hit an issue with our mobile app (home screen safari app) in that it started reporting since iOS 13 that there was no html5 database support.

It tries to detect support for WebSQL with the following code:

function getDBType() {
  if (typeof window.openDatabase == "function") {
    return "WebSQL";
  }
  return "IndexedDB";
}

Unfortunately this is reporting that, despite WebSQL being removed from Safari 13, WebSQL is available, only to fall over with an exception later when it tries to call window.openDatabase().

On closer inspection it seems that something weird is happening:

(typeof window.openDatabase == "function") == true
(typeof window.openDatabase == "undefined") == true
(typeof window.openDatabase == "randomstring") == false
window.openDatabase == [object Function]  /* not native function */

Test Page: http://locutus.sorcerer.co.uk/demo/safari-openDatabase.html

So the question is, how to properly test for WebSQL support that will work on safari 13?

One thought is to do the following to detect Safari 13's lack of support (with comment explaining the seeming pointless test)

if (typeof window.openDatabase == "function" && typeof window.openDatabase != "undefined") {
  return "WebSQL";
}

Side Note: It is possible to enable WebSQL in Safari 13 on iOS is settings, Safari -> Advanced -> Experimental Features -> disable the 'Disable WebSQL' option. Only works in Safari app though, home screen safari apps still don't get WebSQL support.


Solution

  • Our code uses if (!window.openDatabase) throw 'No WebSQL support'; and that seems to be enough to detect the disabled setting on iOS13