Javascript – How to detect users on an iPhone with “Private Browsing” enabled

iphonejavascriptjquery-mobile

My jquerymobile App requires the use of localStorage and sessionstorage e.t.c, I've been giving a prompt to users without cookie support and telling them to enable cookies, but if a user has private browsing enable, this create cookie test I'm doing doesn't work and they just get a still erroneous screen, does anyone know how I could test if the user has private browsing enabled?

Thanks

Best Answer

I don't have an Iphone to test this on, but in the desktop Safari browser (in private mode) running the below function does catch the error and handles it as expected.

function storageEnabled() {
    try {
        localStorage.setItem("__test", "data");
    } catch (e) {
        if (/QUOTA_?EXCEEDED/i.test(e.name)) {
            return false;
        }
    }
    return true;
}

if (!storageEnabled()) alert('localStorage not enabled');

Jsfiddle: http://jsfiddle.net/B9eZ5/