如何在iOS 11+;Safari以及Safari的旧版本?

如何在iOS 11+;Safari以及Safari的旧版本?,ios,browser,safari,ios11,incognito-mode,Ios,Browser,Safari,Ios11,Incognito Mode,我需要检测用户是否在Safari中处于私有模式,无论是在旧版本中还是在IOS 11上。是否有一个测试可以同时涵盖这两个方面 更新:这是一支基于jeprubio下面的解决方案尝试将存储和openDatabase try-catch块结合起来的笔 var isPrivate = false; // Check private in iOS < 11 var storage = window.sessionStorage; try { console.log('first try for

我需要检测用户是否在Safari中处于私有模式,无论是在旧版本中还是在IOS 11上。是否有一个测试可以同时涵盖这两个方面

更新:这是一支基于jeprubio下面的解决方案尝试将存储和openDatabase try-catch块结合起来的笔

var isPrivate = false;

// Check private in iOS < 11
var storage = window.sessionStorage;
try {
  console.log('first try for storage')
    storage.setItem("someKeyHere", "test");
    storage.removeItem("someKeyHere");
} catch (e) {
  console.log('first catch')
    if (e.code === DOMException.QUOTA_EXCEEDED_ERR && storage.length === 0) {
        isPrivate = true;
   } 
}

// Check private in iOS 11: https://gist.github.com/cou929/7973956#gistcomment-2272103
try {
  console.log('second try for opendb');
   window.openDatabase(null, null, null, null);
} catch (e) {
  console.log('second catch');
   isPrivate = true;
}

console.log('isPrivate: ' + isPrivate)

alert((isPrivate ? 'You are' : 'You are not')  + ' in private browsing mode');
var isPrivate=false;
//在iOS<11中检查私有
var存储=window.sessionStorage;
试一试{
console.log('首次尝试存储')
设置项(“someKeyHere”、“test”);
storage.removietem(“someKeyHere”);
}捕获(e){
console.log('first catch')
如果(e.code==DOMException.QUOTA\u超出了\u ERR&&storage.length==0){
isPrivate=真;
} 
}
//在iOS 11中检查private:https://gist.github.com/cou929/7973956#gistcomment-2272103
试一试{
log('opendb的第二次尝试');
openDatabase(null,null,null,null);
}捕获(e){
log('secondcatch');
isPrivate=真;
}
console.log('isPrivate:'+isPrivate)
警报((iPrivate?'You are':'You note')+'在私人浏览模式下”);


在Safari新版本(11+)的正常浏览器模式下,控制台上的openDatabase测试不会出错,但会进入第二个catch,并且isPrivate设置为true。因此,在Safari 11+中,非私有模式也被检测为私有模式。

localStorage
在macOS和iOS上的私有浏览中都不起作用

如果您尝试
setItem
,它将抛出一个错误。这将允许您检测私人浏览

您可以阅读更多关于它的信息

尝试以下方法:

var isPrivate = false;

// Check private in iOS < 11
var storage = window.sessionStorage;
try {
    storage.setItem("someKeyHere", "test");
    storage.removeItem("someKeyHere");
} catch (e) {
    if (e.code === DOMException.QUOTA_EXCEEDED_ERR && storage.length === 0) {
        isPrivate = true;
   } 
}

// Check private in iOS 11: https://gist.github.com/cou929/7973956#gistcomment-2272103
try {
   window.openDatabase(null, null, null, null);
} catch (_) {
   isPrivate = true;
}

alert((isPrivate ? 'You\'re' : 'You aren\'t')  + ' in private browsing mode');
var isPrivate=false;
//在iOS<11中检查私有
var存储=window.sessionStorage;
试一试{
设置项(“someKeyHere”、“test”);
storage.removietem(“someKeyHere”);
}捕获(e){
如果(e.code==DOMException.QUOTA\u超出了\u ERR&&storage.length==0){
isPrivate=真;
} 
}
//在iOS 11中检查private:https://gist.github.com/cou929/7973956#gistcomment-2272103
试一试{
openDatabase(null,null,null,null);
}接住{
isPrivate=真;
}
警报((isPrivate?'You're':'You't')+'在私人浏览模式下”);

为什么要检测私有模式?需要向用户发送一条消息,说明他们处于私有模式,并且可能会遇到一些限制。在latst iOS 11.2.2 Safari中,似乎将私有模式和非私有模式都检测为私有模式。我在这支笔中使用了它,所以它可以快速测试。测试窗口.openDatabase(null,null,null,null)不应在Safari非私有浏览器模式下引发异常,但会引发异常(异常代码18)。