Question

I have this simple utility code to check for browser User-Agent:

public class UserAgentHelper {
    public static boolean isMobile(){
        String userAgent = getUserAgent();
        return userAgent.contains("ipad");
    }
    public static boolean isAndroid(){
        return getUserAgent().contains("android");
    }
    public static native String getUserAgent() /*-{
        return navigator.userAgent.toLowerCase();
    }-*/;
}

I need to check if the browser is mobile or desktop. That is the main point. I'm starting with checking if the browser is iPad browser however this code fails sometimes when the string "ipad" is not there, when I tested on such browser sometimes the keyword is applewebkit, but who knows there could be more. So is there a

  • GWT library for this?
  • or a Javascript library for this?
Was it helpful?

Solution 2

Try

    //Detect iPhone
    navigator.platform.indexOf("iPhone") != -1

    //Detect iPod
    navigator.platform.indexOf("iPod") != -1

For more info have a look at Detect iPad

OTHER TIPS

There is a specialized library called UADetector which contains an extensive set of User-Agent entries.

Below is an example of using the UADetector inside a Servlet

import java.io.IOException;
import javax.servlet.*;
import net.sf.uadetector.service.UADetectorServiceFactory;
import net.sf.uadetector.UserAgent;
import net.sf.uadetector.UserAgentStringParser;

public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_OK);

    PrintWriter out = response.getWriter();

    // Get an UserAgentStringParser and analyze the requesting client
    UserAgentStringParser parser = UADetectorServiceFactory.getResourceModuleParser();
    ReadableUserAgent agent = parser.parse(request.getHeader("User-Agent"));

    out.append("You're a <em>");
    out.append(agent.getName());
    out.append("</em> of type <em>");
    out.append(agent.getType().getName());
    out.append("</em>!");
    }

}

The functionality you are looking for is in the ReadableUserAgent.getType() method. Also, take a look inside the OperatingSystemFamily class (and optionally UserAgentFamily) to see the exact list of agents you might be interested in.

NOTE: if you're using Maven for dependency management, here is the dependency you need to add to you pom.xml file.

NOTE: the UADetectorServiceFactory can also return an instance of UserAgentStringParser that updates daily with new User-Agents, otherwise it will just check the entries the library came with.

Related to GWT we use in our application MGWT osDetection and Window's UserAgent (done by @ManuelCarrasco). Here is a snippet (notice that it could be useful knowing if Ipad has IOS7):

  public static String ua = GWT.isClient() ? Window.Navigator.getUserAgent().toLowerCase() : "JVM";

  public static boolean isUA_AndroidTablet() {
    return ua.contains("android") && !ua.contains("mobile");
  }

  public static boolean isMac = ua.matches(".*(ipad|macintosh).*");

  public static boolean isFF = ua.contains("firefox");

  public static boolean isIE = ua.contains("msie");

  public static boolean isIE8 = ua.contains("msie 8");

  public static boolean isIE9 = ua.contains("msie 9");

  public static boolean isChrome = ua.contains("chrome");

  //"Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11A465"
  //"Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11A501"
  public static boolean isIOS7 = ua.contains(" os 7_");

  public static final OsDetection os = GWT.isClient() ? MGWT.getOsDetection() : null;

  public static final boolean isPhone = GWT.isClient() && (os.isPhone() || os.isRetina());

  public static final boolean isIPad = GWT.isClient() && (os.isIPad() || os.isIPadRetina());

  public static final boolean isIpadOrIphone = isPhone || isIPad; 

Also if you do not want to include MGWT dependency, next code should do the trick:

public static String ua = GWT.isClient() ? Window.Navigator.getUserAgent().toLowerCase() : "JVM";
public static boolean isPad = ua.matches(".*(ipad).*");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top