7

I want to detect if a mobile device is an Tablet (iPad) or not in ASP.net I had a look at 51degrees project but the function to detect a tablet isn't available in the free version - and since we distribute our ASP.net solution to 100's of customers, we cannot buy a 51degrees license for all of them.

Are there any free or open source alternatives for 51degrees available? Or will newer versions of MVC (4?) provide more information in detail than the plain IsMobileDevice()?

Thanks, Konrad

Konrad
  • 4,329
  • 10
  • 54
  • 88

4 Answers4

10

You can request the user agent and check to see if it contains 'ipad' like so

bool isIpad = Request.UserAgent.ToLower().Contains("ipad");
Danny Brady
  • 1,895
  • 4
  • 20
  • 30
  • Not working anymore.... https://stackoverflow.com/a/5155680/4836581 Use userAgent.Contains("ipad;") || userAgent.Contains("macintosh;") – Zvi Redler Feb 28 '22 at 09:44
4

You don't need to "detect an iPad". Just use Media Queries to give you support for the iPad as the Safari browser that comes with iPad already understands CSS3:

CSS3 Media Queries

/* iPads (landscape) */
@media screen and (min-device-width : 768px) 
    and (max-device-width : 1024px) and (orientation : landscape) {
   ...
}
/* iPads (portrait) */
@media screen and (min-device-width : 768px) and (max-device-width : 1024px) 
    and (orientation : portrait) {
   ...
}

Your best bet is to use HTML5Boilerplate as it fixes some other things for iPad also. Use Modernizr for feature detection; it comes with HTML5Boilerplate.

HTML5 Boilerplate

20 Snippets You should be using from Html5 Boilerplate

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • 14
    We need a server-side detection of the mobile device since we route to a complete different page depending on smartphone, tablet, desktop ... Therefore, media-queries are nice to display the same content in another format but not suitable for delivering complete different content. – Konrad Jul 06 '12 at 05:55
  • You can easily display different content with CSS and it is the more dependable way of doing it. Toggle div visibility with display:block and display:none. Agent sniffing will often let you down when you need it most. – IrishChieftain Jul 06 '12 at 11:55
  • I know - but we provide a totally different navigation structure for both phone and desktops. Displaying many content on one page for desktop results in many differently connected pages for mobile phones ... media queries are nice but they are not the swiss army-knife ;-) – Konrad Jul 06 '12 at 14:24
  • Then detect with jQuery and send a value back to the server for the redirect: http://bit.ly/LTM58J – IrishChieftain Jul 06 '12 at 20:41
0

We now user the old Mobile Device Browser File: http://mdbf.codeplex.com/

And extend it with our own browser file as it was described in this solution: http://www.hanselman.com/blog/MixMobileWebSitesWithASPNETMVCAndTheMobileBrowserDefinitionFile.aspx

Now we can even define own parameters to detect, e.g., the compatibility mode of the internet explorer or define an iPad as a non-mobile device.

Konrad
  • 4,329
  • 10
  • 54
  • 88
-2

Here is a total solution for you. This is a site that was built to showcase responsive design using ASP.NET MVC; there is an article, tutorial and complete project download with source code.

http://edcharbeneau.github.com/FoundationSinglePageRWD/

Ed Charbeneau
  • 4,501
  • 23
  • 23