I've recently launched a web app written in C#/.net 4.0 making extensive use of jQuery & jQuery UI to give the best possible user e开发者_运维知识库xperience.
However some users have reported problems when using the site through an iPhone or Android devices.
What is the best accepted method of detecting both iOS and Android? so that I can then tweak the UI for each browser.
Generically speaking, you should detect the browser server side, and then render the correct templates/viewport to the browser. Check the user agent string sent to the server, by the browser - iOS generally contains the substring, "iPhone" and Android uses "Android" followed by the version number ("Android 2.2")
I think the best solution is to try the string
Request.ServerVariables["HTTP_USER_AGENT"]
That will return the word android for android devices so far. Windows is easy to detect. Somebody help me with the other fruit.
See more detailed solution at http://www.codeproject.com/Articles/34422/Detecting-a-mobile-browser-in-ASP-NET
Here's an ASP.NET extension that (apparently, I haven't used it) does all that for you :)
http://51degrees.codeplex.com/
IsMobileDevice does not give correct response on my Android phone. Here is the code I have used.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ " Is MobileDevice ? " + browser.IsMobileDevice + "\n"
+ " screen Height " + browser.ScreenCharactersHeight + "\n"
+ " screen width " + browser.ScreenCharactersWidth + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " +
browser.EcmaScriptVersion.ToString() + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls
+ "\n"
+ "Supports JavaScript Version = " +
browser["JavaScriptVersion"] + "\n";
TextBox1.Text = s;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"
Width="955px" Rows="22" TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Find browser" Font-Size="Large" Width="145px" />
</div>
</form>
</body>
</html>
You can try the code at www.abcd.com.au/browser.aspx with your mobile phone or other device.
精彩评论