Mostrando entradas con la etiqueta android. Mostrar todas las entradas
Mostrando entradas con la etiqueta android. Mostrar todas las entradas

2011/05/20

Detectando la rotación de pantalla en Android

Snippet para detectar cambios en la orientación de pantalla en dispositivos móviles. Está pensado para Android, pero supongo que serviría para cualquier plataforma móvil. Realmente no controla que se rote, si no que controla si existe el evento de "onorientationchange" y asigna una función a él. En el caso de que el evento no esté soportado, vincula el evento a resize.

// Detect whether device supports orientationchange event,
// otherwise fall back to the resize event.
var supportsOrientationChange = "onorientationchange" in window,
    orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

window.addEventListener(orientationEvent, function() {
    alert('HOLY ROTATING SCREENS BATMAN:' + window.orientation + " " + screen.width);
}, false);

Como guinda del pastel, en el caso de que se vincule al evento "resize", se podrían definir 2 variables globales (al contexto) llamadas por ejemplo "screenW" y "screenH" que se controlan en el evento de modo que si están intercambiadas, se ha cambiado la orientación. Con ello, nuestro código quedaría así.

var _screenW = screen.width;
var _screenH = screen.height;

// Detect whether device supports orientationchange event,
// otherwise fall back to the resize event.
var supportsOrientationChange = "onorientationchange" in window,
    orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

window.addEventListener(orientationEvent, function() {
    if(supportsOrientationChange ||
      (!supportsOrientationChange && _screenW == screen.height && _screenH == screen.width))
   {
       alert('HOLY ROTATING SCREENS BATMAN:' + window.orientation + " " + screen.width);
   }
}, false);

2011/03/29

Detección de clientes móviles mediante javascript, PHP y .htaccess

Recopilo las forma de detectar navegadores de dispositivos móviles mediante distintas tecnologías:

Javascript
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; // && ua.indexOf("mobile");
var isiPhone = ua.indexOf("iphone") > -1;
var isiPod = ua.indexOf("ipod") > -1;
var isiPad = ua.indexOf("ipad") > -1;
// For use within iPad developer UIWebView
if(!isiPad) isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
var isWPx = ua.indexOf("windows phone") > -1;
PHP
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$isAndroid = stripos($ua, 'android') !== false; // && stripos($ua,'mobile') !== false
$isiPhone = stripos($ua, 'iphone') !== false;
$isiPod = stripos($ua, 'ipod') !== false;
$isiPad = stripos($ua, 'ipad') !== false;
$isWPx = stripos($ua, 'windows phone') !== false;
.htaccess
RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$ RewriteRule ^(.*)$ http://android.site.com [R=301] RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$ RewriteRule ^(.*)$ http://iphone.site.com [R=301] RewriteCond %{HTTP_USER_AGENT} ^.*iPod.*$ RewriteRule ^(.*)$ http://ipod.site.com [R=301] RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$ RewriteRule ^(.*)$ http://ipad.site.com [R=301]RewriteCond %{HTTP_USER_AGENT} ^.*Windows Phone.*$RewriteRule ^(.*)$ http://wpx.site.com [R=301]
HTML

<!--[if IEMobile]>
    Explorer mobile en Windows Phone 7
<![endif]-->
<![if !IEMobile]>
    Otros
<![endif]>