无法在 iOS 上显示不同的内容

Posted

技术标签:

【中文标题】无法在 iOS 上显示不同的内容【英文标题】:Trouble showing different content on iOS 【发布时间】:2020-04-10 22:46:17 【问题描述】:

2 天后,我把自己逼疯了,因为我无法解决这个问题。我的问题如下: 1.) 我正在尝试使用 javascriptios 设备为目标(无论浏览器是苹果上的 Chrome、Safari、苹果上的 Firefox 等)来显示与非 iOS 浏览器不同的 DOM 元素。 2.) 我的iOS检测代码如下,我在其他几个线程中看到过: // Detects if device is on iOS

const isIos = () => 
        const userAgent = window.navigator.userAgent.toLowerCase();
        return /iphone|ipad|ipod/.test( userAgent );
    

3.) 运行此检查后,我在 iOS 上的 Safari 和 Chrome 中尝试以下操作“我假设的方式不正确,因为它无法正常工作,哈哈!” :

if(!isIos()) 
                setTimeout(function() 
                    console.log('showing non iOS banner prompt after delay');
                , 10000);
             else if (isIos()) 
                setTimeout(function() 
                    console.log('showing iOS banner prompt after delay');
                , 10000);
            

4.) 选择 iOS 设备(我知道这不是实际的 iOS 环境)时,通过我的 windows 笔记本电脑上的 chrome 开发工具显示正确的日志,但无论我做什么,一旦加载到实时站点它总是在我的 iPad 上显示非 Ios 日志。

【问题讨论】:

【参考方案1】:

已解决(适用于 iPad) 找到这个帖子enter link description here后,我改变了我的检测方法。我在 iPAD 上进行测试(因为这是我唯一拥有的 Apple 产品,因为我不是粉丝),在更正之后,我当前的问题得到了解决。我希望这也适用于其他设备,但在我借朋友之前我不会知道。希望其他人觉得这很有帮助。

||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)

const isIos = () => 
        const userAgent = window.navigator.userAgent.toLowerCase();
        return /iphone|ipad|ipod/.test( userAgent ) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
    

【讨论】:

以上是关于无法在 iOS 上显示不同的内容的主要内容,如果未能解决你的问题,请参考以下文章