How do I add my own detection method?

Comments

awm’s picture

So I needed to detect ipad user agent and treat it as a mobile device to load an appropriate theme. Here is what I did:
1. Implement hook_mobile_theme_detection in the following manner:

function mymodule_mobile_theme_detection() {
  return array(
    'my_function_to_detect_ipad' => 'detect ipad',
  );
}

2. implement in the following manner


function my_function_to_detect_ipad() {
  $mobile_browser = 0;
 // note the ipad in the end in the condition 
  if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|pad)/i',
strtolower($_SERVER['HTTP_USER_AGENT']))) { 
    $mobile_browser++;
  }
  if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
    $mobile_browser++;
  }
  $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
  $mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda ','xda-');
  if (in_array($mobile_ua,$mobile_agents)) {
    $mobile_browser++;
  }
  if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']), 'OperaMini') > 0) {
    $mobile_browser++;
  }
  if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') > 0) {
    $mobile_browser = 0;
  }
  return $mobile_browser > 0;
}


Then choose detect ipad from the detection settings under appearance/ settings.

HTH others

awm’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

caspervoogt’s picture

Thanks. This worked for me, as I needed to detect smartphones AND iPads, not just smartphones only.

charlysole’s picture

Issue summary: View changes

Implement ok... but where?
Thanks