我正在使用C# asp.net网站。
如何检查用户是否使用ipad或iphone?如何检查平台?
例如,如果用户从iPad输入网站,则希望显示“Hello ipad user”。
我正在使用C# asp.net网站。
如何检查用户是否使用ipad或iphone?如何检查平台?
例如,如果用户从iPad输入网站,则希望显示“Hello ipad user”。
2020年7月17日更新: 看起来苹果已经将iPad
一词删除,现在使用Macintosh
代替
更新:
由于iPad用户代理包含@Rob Hruska 提到的iPhone
一词:
Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10
而iPhone用户代理大致是这样:
Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
因此,正确的方法是检查是否包含iPhone;
或iPad;
来识别设备:
var userAgent = HttpContext.Current.Request.UserAgent.ToLower();
if (userAgent.Contains("iphone;"))
{
// iPhone
}
else if (userAgent.Contains("ipad;") || userAgent.Contains("macintosh;"))
{
// iPad
}
else
{
// Think Different ;)
}
而针对iPhone则是这样的:Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10
还有许多其他版本,无论是iPhone 3还是4,因此最好只进行iPhone和iPad的子字符串搜索,如另一个答案所建议的。Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3
您可以通过获取用户代理来实现。
string ua = Request.UserAgent;
if (ua != null && (ua.Contains("iPhone") || ua.Contains("iPad")))
{
...
...
...
}
我建议先尝试使用WURFLhttp://wurfl.sourceforge.net/。
他们有.NET API和非常好的代码示例。http://wurfl.sourceforge.net/dotnet_index.php
可以帮助您的类名为WURFLManager,具有以下方法:
使用WURFL http://wurfl.sourceforge.net/dotnet_index.php
如果您使用asp.net mvc,可以使用ActionFilter
public class MobileActionFilterAttribute : ActionFilterAttribute
{
// The WURFL database contains information about a huge number of devices and mobile browsers.
// http://wurfl.sourceforge.net/
// http://wurfl.sourceforge.net/dotnet_index.php
// http://wurfl.sourceforge.net/help_doc.php
private static readonly IWURFLManager WurflManager;
static MobileActionFilterAttribute ()
{
IWURFLConfigurer configurer = new ApplicationConfigurer();
WurflManager = WURFLManagerBuilder.Build(configurer);
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.RequestContext.HttpContext.Request;
// We don't have ARR server for dev environment, so we still need to check to see if the current domain name is the mobile site.
if (request.Url.AbsoluteUri.StartsWith(SiteConfiguration.Current.MobileSiteAddress, StringComparison.OrdinalIgnoreCase))
{
return;
}
// Creates a WURFLRequest object from an ASP.NET HttpRequest object
WURFLRequest wurflRequest = WURFLRequestFactory.CreateRequest(HttpContext.Current.Request);
// Indicates whether the current user agent string refers to a desktop agent.
if (wurflRequest.IsDesktopRequest)
return;
// Get the information about the device
IDevice deviceInfo = WurflManager.GetDeviceForRequest(wurflRequest);
// Tells you if a device is a tablet computer (iPad and similar, regardless of OS)
bool isTablet = string.Equals(deviceInfo.GetCapability("is_tablet") ?? string.Empty, "true", StringComparison.OrdinalIgnoreCase);
if (isTablet)
{
// so we don't show the mobile site for iPad.
return;
}
// Indicates whether the current user agent string refers to a mobile device.
bool isMobileRequest = wurflRequest.IsMobileRequest;
// Tells you if a device is wireless or not. Specifically a mobile phone or a PDA are considered wireless devices, a desktop PC or a laptop are not
bool isWirelessDevice = string.Equals(deviceInfo.GetCapability("is_wireless_device") ?? string.Empty, "true", StringComparison.InvariantCultureIgnoreCase);
if (isMobileRequest && isWirelessDevice)
{
// we can redirect to the mobile site!
filterContext.Result = new RedirectResult(SiteConfiguration.Current.MobileSiteAddress);
}
}
}
还有51Degrees.Mobi。Steve Sanderson在他的博客中介绍了如何使用它。
51Degrees.Mobi Foundation是一个开源.NET项目,它增强了Request.Browser,使其从无线通用资源文件(WURFL)获取信息 - 这是最全面和最新的移动设备信息数据库之一。令人兴奋的消息是,现在可以将51Degrees.Mobi Foundation作为NuGet包使用,因此安装和更新非常容易。
注意Windows手机!出于某种奇怪的原因,许多Windows手机在用户代理中会显示“像iPhone”。因此,您需要检查:
public bool IsIPhone
{
get
{
if (!UserAgent.ToUpper().Contains("LIKE IPHONE"))
{
return UserAgent.ToUpper().Contains("IPHONE");
}
return false;
}
}
示例Windows手机用户代理(来自Lumia 735):
"Mozilla/5.0(移动设备;Windows Phone 8.1;Android 4.0;ARM;Trident/7.0;Touch;rv:11.0;IEMobile/11.0;NOKIA;Lumia 735)like iPhone OS 7_0_3 Mac OS X AppleWebKit/537(KHTML,like Gecko)Mobile Safari/537"
从 iOS 13 开始,用户代理变成了Mac OS,例如:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15
"Original Answer" 的翻译是 "最初的回答"。
你可以从Request.UserAgent
获取客户端操作系统数据,包括操作系统名称和版本。
public static string GetClientOS(string ua, string platform)
{
if (ua.Contains("Android"))
return string.Format("Android {0}", GetMobileVersion(ua, "Android"));
if (ua.Contains("iPad"))
return string.Format("iPad OS {0}", GetMobileVersion(ua, "OS"));
if (ua.Contains("iPhone"))
return string.Format("iPhone OS {0}", GetMobileVersion(ua, "OS"));
if (ua.Contains("Linux") && ua.Contains("KFAPWI"))
return "Kindle Fire";
if (ua.Contains("RIM Tablet") || (ua.Contains("BB") && ua.Contains("Mobile")))
return "Black Berry";
if (ua.Contains("Windows Phone"))
return string.Format("Windows Phone {0}", GetMobileVersion(ua, "Windows Phone"));
if (ua.Contains("Mac OS"))
return "Mac OS";
if (ua.Contains("Windows NT 5.1") || ua.Contains("Windows NT 5.2"))
return "Windows XP";
if (ua.Contains("Windows NT 6.0"))
return "Windows Vista";
if (ua.Contains("Windows NT 6.1"))
return "Windows 7";
if (ua.Contains("Windows NT 6.2"))
return "Windows 8";
if (ua.Contains("Windows NT 6.3"))
return "Windows 8.1";
if (ua.Contains("Windows NT 10"))
return "Windows 10";
return platform + (ua.Contains("Mobile") ? " Mobile " : "");
}
public static string GetMobileVersion(string userAgent, string device)
{
var temp = userAgent.Substring(userAgent.IndexOf(device) + device.Length).TrimStart();
var version = string.Empty;
foreach (var character in temp)
{
var validCharacter = false;
int test = 0;
if (int.TryParse(character.ToString(), out test))
{
version += character;
validCharacter = true;
}
if (character == '.' || character == '_')
{
version += '.';
validCharacter = true;
}
if (validCharacter == false)
break;
}
return version;
}
private static final Pattern IPHONE_AGENT = Pattern.compile(".*iPad.*|.*iPhone.*|.*iPod.*");
String userAgent = request.getHeader("User-Agent");
if (userAgent != null && IPHONE_AGENT.matcher(userAgent).matches()) {
// do something
}