Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Mobile module - adding iPad detection


Reply

5 Posts   2303 Views

Avatar
badjedi

16 September 2010 at 4:18pm Community Member, 25 Posts

I have added a bit of code to the new mobile module to detect iPads and NOT send them to the mobile site/theme since the iPad displays most webpages fine.

I've tested it on my iPad 32G wifi, and double checked that it still displayed the mobile theme on my iphone.

1. So to get started I added the following code into MobileBrowserDetector.php at line 29:

   public static function is_ipad() {
      return (preg_match('/ipad/i', $_SERVER['HTTP_USER_AGENT'])) ? false : false;
   }

2. then in MobileBrowserDetector.php at line 69:

case(self::is_ipad()):
            $isMobile = true;
            break;

3. Then had to remove "mobile |" from line 66, so it reads:

case(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|pda|psp|treo)/i', $agent)):
            $isMobile = true;
            break;

4. added the following code to MobileSiteControllerExtension.php on line 87:

   public function IsiPad() {
      return MobileBrowserDetector::is_ipad();
   }

I'm sure that I might have messed up a few thigns by removing "mobile |" in step 3, for other devices, but I wanted to serve the normal site to iPads, and the mobile version to iPhones. I have tested and this has worked. Im not sure about other mobile devices.

In case any one wants the iPad User agent string here it is:

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

Im not the best a php, and would be interested if anyone could help me add how to serve an iPad specific template to the iPad, and iPad only.

Or it any one know s a better way to do what i just did please help :)

you can see the results on the running simple website www.trackhopper.co.nz

Avatar
Gene

22 October 2010 at 5:19am Community Member, 41 Posts

Instead of removing "mobile" just put your ipad case statement above the mobile case statement so it gets called first. That way it'll skip that check if it detects an ipad and you won't lose support for other mobile devices.

Avatar
badjedi

22 October 2010 at 2:24pm Community Member, 25 Posts

Thanks gene,

That worked.

I put mobile back into MobileBrowserDetector.php line 66.

But i had to actually move the ipad case statement to the bottom to get it to work in stead of the top.

Thanks!

Avatar
Tadeck

7 May 2011 at 4:04am Community Member, 1 Post

Hello, badjedi,

Have you noticed, the following code is pointless? Instead of:

public static function is_ipad() {
return (preg_match('/ipad/i', $_SERVER['HTTP_USER_AGENT'])) ? false : false;
}

it should look like:

public static function is_ipad() {
return (preg_match('/ipad/i', $_SERVER['HTTP_USER_AGENT'])) ? true : false;
}

(watch for the boolean change)

Avatar
Nivanka

10 May 2011 at 2:03pm Community Member, 395 Posts

I did some work on this module as well, and there are quite a lot of new features, like to add specific templates are specific designs etc.

find out the fork here https://github.com/fonsekaean/silverstripe-mobile