[英]How to detect if device is iOS, Android or Desktop
我需要根据用户操作系统显示不同的应用程序链接。 如果用户在桌面上打开页面,例如 IOS、Android 或两者。 我正在使用 React 和 Next.js 我尝试使用 navigator.userAgent 但没有成功。
实现如下代码就完美了:
import React from "react";
import { AppDownload as AppDownloadProps } from "xxx";
export default function DownloadApp({
appDownload,
}: AppDownloadProps): JSX.Element {
return (
<div>
{isIos && <a href={appDownload?.ios.url}></a>}
{isAndroid && <a href={appDownload?.android.url}></a>}
{isDesktop && <a href={appDownload?.desktop.url}></a>}
</div>
);
}
Detect MacOS、iOS、Windows、Android 和 Linux OS with JS的可能副本。
您可以使用window.navigator
但需要解析结果。
您可以使用navigator.userAgent
来检测浏览器中的用户代理,还有许多其他选项可用于获取代理信息。
console.log("agent is:", window?.navigator?.userAgent) console.log("platform is: ", window?.navigator?.platform) console.log("user agent data: ", window?.navigator?.userAgentData)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.