Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if device is iOS or Android, and then redirect

Tags:

So I have a website, and what I want is that when the open my website: www.example.com/download.html I want to first detect if the device is iOS device or Android device, and then redirect to another link, for example www.google.com (just an example). I want different link for different OS. Any tips on how I can manage this? :)

like image 383
Ian Swendee Avatar asked Aug 18 '18 17:08

Ian Swendee


People also ask

How do I know if my device is iOS or Android?

On your device, go to the Home screen (the one with all the icons), and tap on the Settings icon. Scroll down and tap on About phone or About tablet. Some information will appear. If one of the lines of information says Android with a version number, you have an Android device.

How to check if device is mobile in JavaScript?

matchMedia() The Window interface's matchMedia() method returns a new MediaQueryList object that can then be used to determine if the document matches the media query string. For example, a device whose screen width does not exceed 768px is considered a mobile device: const isMobile = window.


2 Answers

You can use the user agent string to detect different kinds of devices like so:

function androidOrIOS() {
    const userAgent = navigator.userAgent;
    if(/android/i.test(userAgent)){
        return 'android';
    }
    if(/iPad|iPhone|iPod/i.test(userAgent)){
        return 'ios';
    }
}
like image 154
Alex C Avatar answered Sep 28 '22 17:09

Alex C


You can use Navigator to determine the type of device

function navigate() {
    if((/Mobi|Android/i.test(navigator.userAgent))){
        window.location.href = 'android url ';
    }
    if(/Mobi|iPad|iPhone|iPod/i.test(navigator.userAgent)){
        window.location.href = 'ios url ';
    }
}
like image 37
Madhan Varadhodiyil Avatar answered Sep 28 '22 17:09

Madhan Varadhodiyil