locked
detecting tablets as opposed to other mobile devices RRS feed

  • Question

  • User745803033 posted

    I know that asp.net provides code to detect mobile devices, but is there a way to detect tablets vs mobile devices?  I don't want the mobile version of my website to be shown if the tablet has a big enough screen, and most tablets do.  Right now I have:

     Protected Sub Page_PreInit(sender As Object, e As System.EventArgs) Handles Me.PreInit
            If IsItMobile() Then
                Response.Redirect("/mobile/m_home.aspx")
            End If
        End Sub
    
        Function IsItMobile() As Boolean
            Dim myBrowserCaps As System.Web.HttpBrowserCapabilities = Request.Browser
            If (CType(myBrowserCaps, System.Web.Configuration.HttpCapabilitiesBase)).IsMobileDevice Then
                Return True
            Else
                Return False
            End If
        End Function 

    Is there any way to make "IsItMobile" return false for tablets such as the iPad, the Samsung galaxy, and windows Surface?

    I had thought that perhaps JavaScript could examine the user agent string and then store its result in a hidden field, which I could pick up in the code-behind page.  One problem with that though is that some phones have JavaScript disabled.

    There is also a library produced by 51 degrees, which is an encyclopedia of all types of devices and their sizes, but I'm first looking for a free solution.

    Thanks.

    Saturday, June 13, 2015 6:26 PM

Answers

  • User281315223 posted

    Another option might be to consider using something like WURFL, which is a mobile device database that can help you detect certain information and features about your devices. It also features a client-side option that could be used to help manage your redirection, which is probably going to be the easiest way to handle this in wurfl.io.

    Basically, if you aren't going to be able to access something like 51 Degrees, you'll want to handle this client side. A client-side approach is probably going to be your best option in general as properties like the size of your viewport are going to be exposed and you can use those to handle if you need to redirect to a mobile site or not.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 14, 2015 2:01 PM

All replies

  • User281315223 posted

    Consider using a Library

    You may want to consider taking a look at 51Degree's "Foundation" project (also called the 51Degrees.Mobi project) , which greatly enhances the Mobile detection features available in .NET and is actually recommended by ASP.net as it provides better detection for more recent devices. You can read more about the Foundation project and download it here as the "Lite" version should still be free for most use-cases :

    The reason that I mention this approach is that the built-in detection for mobile devices within ASP.NET can be unreliable or inaccurate and the use of a library like this would strongly improve those features.  

    I had thought that perhaps JavaScript could examine the user agent string and then store its result in a hidden field, which I could pick up in the code-behind page.  One problem with that though is that some phones have JavaScript disabled.

    You can still access the actual UserAgent string within your server-side code using the Request.UserAgent property. You could essentially use this to apply any Javascript logic that you came across to handle detecting mobile devices.

    Saturday, June 13, 2015 9:20 PM
  • User745803033 posted

    They don't seem to have a way to distinguish between tablets and phones, so I was going to use their function that gets the physical size of the screen instead.  But that does not come with the "lite" version.

    Sunday, June 14, 2015 6:07 AM
  • User281315223 posted

    Another option might be to consider using something like WURFL, which is a mobile device database that can help you detect certain information and features about your devices. It also features a client-side option that could be used to help manage your redirection, which is probably going to be the easiest way to handle this in wurfl.io.

    Basically, if you aren't going to be able to access something like 51 Degrees, you'll want to handle this client side. A client-side approach is probably going to be your best option in general as properties like the size of your viewport are going to be exposed and you can use those to handle if you need to redirect to a mobile site or not.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 14, 2015 2:01 PM
  • User745803033 posted

    I found that 51 degrees cloud option does have a free option that does tell you whether you have a tablet or a phone.  However the tech person there did tell me the cloud option takes a little more time to load than if the database is on our own server.

    Thanks for the help.

    Monday, June 15, 2015 10:34 AM