Skip to content

Instantly share code, notes, and snippets.

@AvocadoVenom
Last active July 8, 2022 07:25
Show Gist options
  • Select an option

  • Save AvocadoVenom/26137e4c9af37f6e832602ae084956ae to your computer and use it in GitHub Desktop.

Select an option

Save AvocadoVenom/26137e4c9af37f6e832602ae084956ae to your computer and use it in GitHub Desktop.
enum MobileOS {
Android = 'android',
iOS = 'ios',
Unknown = 'unknown',
WindowsPhone = 'Windows Phone'
}
/**
* https://stackoverflow.com/a/21742107
* Determine the mobile operating system.
*
* @returns {MobileOS}
*/
const getMobileOS = (): MobileOS | undefined => {
if (isMobileDevice()) {
// Windows Phone must come first because its UA also contains "Android"
if (/windows phone/i.test(userAgent)) return MobileOS.WindowsPhone
else if (/android/i.test(userAgent)) return MobileOS.Android
// iOS detection from: http://stackoverflow.com/a/9039885/177710
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) return MobileOS.iOS
return MobileOS.Unknown
} else return undefined
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment