检测用户代理适用于 android 但不适用于 iphone?

Detecting useragent works for android but not iphone?

目标:检测用户是否在iPhone/iPad/Android(移动设备)和运行一些基于该条件的代码

Problem/What 发生:它似乎检测到 android,但没有检测到 iPhone/iPod。

我尝试过的: 我正在使用 Chrome devtools 对此进行测试。我 select 用户代理从 Chrome Devtools 到类似的东西: Chrome - iPhone ,或 Chrome - Android Mobile.

我试过调试这个,我得到: uagent = "mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko) version/9.0 mobile/13b137 safari/601.1"

然而它完全跳过了条件

我也看了不同的文章,我的匹配条件看起来是正确的。事实上,我是从一篇 SO 文章中得到这个的。但它在 W3Schools、mozilla 文档等上看起来是一样的。 我还尝试将代码从服务方法中移回组件本身,但这没有帮助。我记得测试过这个并且它在某个时候工作(使用 iPhone 作为用户代理)但不确定发生了什么变化。

Angular/TypeScript:

this.isMobile = this.securityService.isUserAgentMobile();

isUserAgentMobile(): boolean {
        var isMobile = false;
        var uagent = navigator.userAgent.toLowerCase();
        if (uagent.match(/iPad|iPhone|android/)) {
            console.log("user agent is mobile!");
            isMobile = true;
        }
        return isMobile;
}

由于设置为小写,因此匹配项也必须是小写:

var uagent = navigator.userAgent.toLowerCase();
if (uagent.match(/ipad|iphone|android/))