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 CS开发者_Python百科S and JS files to the page.
Any ideas?
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.
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?
You can use the User Agent to determine if IOS or Android is being used. Just look for the appropriate keywords such as "Android" or "iPhone" or "iPad"
精彩评论