Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect private browsing in iOS 11 Mobile Safari or MacOS High Sierra Safari?

On the new iOS 11 Safari and MacOS High Sierra Safari, that trick of seeing if window.localStorage.setItem('test', 1); (see https://stackoverflow.com/a/17741714/1330341) 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?

like image 564
JYeh Avatar asked Aug 16 '17 19:08

JYeh


People also ask

How do I know if Safari is in private mode iPhone?

You can quickly tell if you are using Private Browsing as the url/search bar will appear with a dark theme instead of white or gray for standard windows. Also, the white highlight around Private shown above on the right) means it is turned on. Tap the + symbol at the bottom middle of the screen and start browsing.

How do I know Safari is in private mode?

Step 1: Open Safari. Step 2: Tap the tab icon at the bottom-right corner of the screen. Note that the top border of a private browsing session is dark gray, while a normal session is light gray. This is a private browsing window.

Does Safari on Mac have private browsing?

In the Safari app on your Mac, choose File > New Private Window, or switch to a private window that's already open. A private window has a dark Smart Search field with white text. Browse as you normally would.

Does private browsing on phone show up on Mac?

How to access incognito mode on Safari on an iPhone. Opening up Private Browsing or “incognito mode” on an iPhone is just as easy as it is on a Mac, and you'll enjoy all the same benefits. Safari won't store any local session data on your iPhone, nor will it sync anything from your private session to iCloud.


2 Answers

I find a solution here:

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

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

Hope it helps :)

like image 80
GOR BAND Avatar answered Sep 24 '22 21:09

GOR BAND


Haven't actually tried it, but from reading Apple's document:

https://support.apple.com/kb/ph21413?locale=en_US

It lists various characteristics of private mode browsing (~snip):

When you use a Private Browsing window:

  • Each tab in the window is isolated from the others, so websites you view in one tab can’t track your browsing in other tabs.

  • Safari doesn’t remember the webpages you visit or your AutoFill information.

  • Safari doesn’t store your open webpages in iCloud, so they aren’t shown when you view all your open tabs from other devices.

  • Your recent searches aren’t included in the results list when you use the Smart Search field.

  • Items you download aren’t included in the downloads list. (The items do remain on your computer.)

  • If you use Handoff, Private Browsing windows are not passed to your iOS devices or other Mac computers.

  • Safari doesn’t remember changes to your cookies or other website data. Safari also asks websites and others who provide those sites with content (including advertisers) not to keep track of your browsing, although it is up to the websites to honor this request.

  • Plug-ins that support Private Browsing stop storing cookies and other tracking information.

From the above, in particular I found interesting that Safari specifically asks websites to "not track" the browsing. This could potentially be a mechanism to look for, to determine if using private browsing.

See this answer for an example:

Implementing Do not track in asp.net mvc

Again, haven't tested and unsure if it will work, but if not the list provides other potential options. HTH.

like image 26
kvr Avatar answered Sep 23 '22 21:09

kvr