Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect mobile (iOS and Android) using JSP/Java?

Just wondering if anyone has come across this?

Basically, Im looking to detect for iOS and Android using JSP and to be able to conditionally add CSS and JS files to the page.

Any ideas?

like image 858
jrutter Avatar asked Dec 17 '22 11:12

jrutter


2 Answers

A very simple solution would be:

<%
  String userAgent = request.getHeader("user-agent");
  if (userAgent.matches(".*Android.*"))
  {
    out.print("You're an Android!");
  }
  else
  {
    out.print("You're something else..."); // iOS
  }
%>

Because of the very short else-statement, this should be used only if you serve no more than iOS and Android.

like image 56
Benny Neugebauer Avatar answered Dec 21 '22 09:12

Benny Neugebauer


Best way would probably be with the User Agent string. There's actually a pretty similar question on SO already, at least for iOS/Safari. Note that there are other browsers on iOS so you will need to look for their user agent strings as well.

Alot of UA strings listed on this site.

How do I detect Mobile Safari server side using PHP?

like image 25
Perception Avatar answered Dec 21 '22 10:12

Perception