Detect private browsing in iOS 11 Mobile Safari

On iOS 11 Safari (and probably the new version of desktop Safari too? but didn't check that one), that trick of seeing if

window.localStorage.setItem('test', 1);

throws an error no longer works, because it no longer throws an error, and it also properly sets the localStorage item. Has anyone figured out any other way to check for private browsing mode in the new versions of Safari?


I also posted here:

https://stackoverflow.com/questions/45721156/detect-private-browsing-in-ios-11-mobile-safari

and

https://gist.github.com/cou929/7973956#gistcomment-2178123

Replies

Here’s how you do it:


window.localStorage.setItem('IAmASurveillanceCapitalist', 1);


Hope that helps. (And stop farming people, it’s not nice.)

not yet

Hi,

We have same problem,

We need get use in Private Mode,

but iOS 11.x can't use Post method to detect it.


Have any method let we know user iOS safari in Private mode?


Example,

Web have auto login, we read cookie or localstorage to do it,

but Private mode will clear it if close page, but user don't know her is Private mode.

I've found an Answear at GitHuband tested it: Working on IOS 11!



    var isPrivate = false;
    try {
      window.openDatabase(null, null, null, null);
    } catch (_) {
      isPrivate = true;
    }
    alert((isPrivate ? 'You\'re' : 'You aren\'t')  + ' in private browsing mode');