'use client'
export const getMobileOS = () => {
if (typeof window === "undefined") return "Other"
const ua = window.navigator.userAgent
if (/android/i.test(ua)) {
return "Android"
}
else if (/iPad|iPhone|iPod/.test(ua) || (/Macintosh/.test(ua) && navigator.maxTouchPoints > 1)) {
return "iOS"
}
return "Other"
}
// iOS の major version を返す。取得できない場合は null。
// iOS 26 以降、Apple は "CPU iPhone OS" トークンを 18_7 で凍結しており
// (フィンガープリンティング対策)、実バージョンは Safari の "Version/26.x" 側にしか
// 出ない。逆に WebView 等では "Version/" が無い。そのため両方を見て大きい方を採用する。
// iPad (iPadOS 13+) は UA が Macintosh 偽装で "iPhone OS" トークンが無く、
// "Version/" のみが手がかりになる。
export const getIOSVersion = (): number | null => {
if (typeof window === 'undefined') return null
const ua = window.navigator.userAgent
const osMatch = ua.match(/OS (\d+)_/)
const versionMatch = ua.match(/Version\/(\d+)/)
const candidates = [osMatch, versionMatch]
.map(m => (m ? parseInt(m[1], 10) : NaN))
.filter(n => !isNaN(n))
if (candidates.length === 0) return null
return Math.max(...candidates)
}
export const getBrowser = (): string => {
if (typeof window === 'undefined') return 'Other'
// 1) iOS WebView判定: window.webkit.messageHandlers.getDevicePushToken があれば true
// 2) Android WebView判定: window.AndroidNative.getDevicePushToken があれば true
if (
(
(window as any).webkit
&& (window as any).webkit.messageHandlers
&& (window as any).webkit.messageHandlers.getDevicePushToken
)
|| (
(window as any).AndroidNative
&& (window as any).AndroidNative.getDevicePushToken
)
) {
return 'WebView'
}
const agent = window.navigator.userAgent.toLowerCase()
if (agent.indexOf('msie') !== -1 || agent.indexOf('trident') !== -1) {
return 'Internet Explorer'
} else if (agent.indexOf('edg') !== -1 || agent.indexOf('edge') !== -1) {
return 'Edge'
} else if (agent.indexOf('opr') !== -1 || agent.indexOf('opera') !== -1) {
return 'Opera'
} else if (agent.indexOf('chrome') !== -1 || agent.indexOf('crios') !== -1) {
// iOS版Chromeは 'crios' を含む
return 'Chrome'
} else if (agent.indexOf('safari') !== -1) {
return 'Safari'
} else if (agent.indexOf('firefox') !== -1) {
return 'FireFox'
} else {
return 'Other'
}
}