Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to only detect if is an iOS devices and redirect?

I'm currently using this to detect if the visitors have flash installed or not and if not right away I'm assuming they are iPhone users but then now it seems it doesn't always work like I wish to because sometime they being redirect to the version of flash of my site and sometime the person who got flash installed also redirect to an iPhone version of my site

<!DOCTYPE html>
<html>
<head>    
     <script src="http://www.featureblend.com/flash_detect_1-0-4/flash_detect.js" type="text/javascript" charset="utf-8"></script> 

<script type="text/javascript"> 
    if(!FlashDetect.installed){
        window.location = "?flash=false";       
    }else{
        window.location = "?flash=true";
    }
    </script>
</head>

the thing is how would I detect if they are iPhone/iPad/iPod/AppleTV something like that and redirect them to the flash=false URL and if they are non of the above will be redirect to flash=true?

I have tried to find but really can't find the exact what I'm looking for

Thank you and Merry X'Mas to all of you.

like image 250
Ali Avatar asked Dec 10 '22 03:12

Ali


1 Answers

There are a lot of tutorials about how to achieve this. You can start from here: http://www.hand-interactive.com/resources/detect-mobile-javascript.htm

Briefly, you can simply look for the strings "iphone", "ipod", "ipad" etc... into the user agent string with a code similar to this:

var uagent = navigator.userAgent.toLowerCase();

if (uagent.search("iphone") > -1 ||
    uagent.search("ipod") > -1 ||
    uagent.search("ipad") > -1 ||
    uagent.search("appletv") > -1) {
   window.location = "?flash=false";       
} else {
   window.location = "?flash=true";
}

Merry Christmas to you too :)

like image 55
Andrea Sprega Avatar answered Dec 22 '22 16:12

Andrea Sprega