【问题标题】:How do I check if the useragent is an ipad or iphone?如何检查用户代理是 ipad 还是 iphone?
【发布时间】:2011-03-01 13:59:00
【问题描述】:

我正在使用 C# asp.net 网站。

如何检查用户使用的是 ipad 还是 iphone?如何查看平台?

例如,如果用户从 ipad 进入网站,我想显示“Hello ipad user”

【问题讨论】:

  • 你能说得更具体点吗?您是否正在使用 MonoTouch 编写 C# 应用程序以部署到这些设备?您是否正在编写将由这些设备访问的 C# ASP .NET 网站?
  • 我猜它不是 C# 应用程序,而是 C# 网络服务器(ASP?)你应该检查 UserAgent
  • 是的,它是 c# asp.net web
  • 维基百科在这里对 UserAgent 进行了介绍:en.wikipedia.org/wiki/User_agent

标签: c# .net iphone asp.net ipad


【解决方案1】:

2020 年 7 月 17 日更新:Apple 似乎删除了 iPad 一词,现在改用 Macintosh

更新: 由于 iPad 用户代理包含单词 iPhone 为 @Rob Hruska mentioned

Mozilla/5.0(iPad;U;CPU iPhone OS 3_2,如 Mac OS X;en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) 版本/4.0.4 Mobile/7B314 Safari/531.21.10

而 iPhone 用户代理类似于 this:

Mozilla/5.0(iPhone;U;CPU iPhone OS 4_0,如 Mac OS X;en-us) AppleWebKit/532.9 (KHTML, like Gecko) 版本/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 ;)
}

【讨论】:

  • 这个解决方案实际上没问题,对我有用,想知道为什么这不是选定的答案...... 呵呵 我只是复制和粘贴,'Alex' 这有帮助..谢谢! :)
  • Yo Alex,它也帮助了我。 +1。
  • 这不会将 iPad 识别为 iPhone,因为 iPad user agent 包含 ... CPU iPhone ...
  • 需要检查 iPad 之前 iPhone,因为 iPad 也包含 iPhone,否则总是会得到 iPhone 作为答案。
  • @Ken-AbdiasSoftware,需要检查iPhone;iPad。顺序无关紧要。
【解决方案2】:

对于 iPad,user agent 类似于:

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 like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3

更多取决于版本和 iPhone 3 或 4

最好按照另一个答案的建议对 iPhone 和 iPad 进行子字符串搜索

【讨论】:

  • 搜索“iPhone”也会将 Windows 手机识别为 iPhone。请参阅下面的答案。
  • 新 iPad 在用户代理中看起来像 Macintosh。见getpolarized.io/2019/12/21/…
  • 这个答案不再正确,因为 iPadOS 默认从用户代理字符串中删除了“iPad”。 Settings -> Safari -> Request Desktop Website -> All websites 现在默认开启。
【解决方案3】:

这些设备的用户代理包括“iPod”、“iPad”或“iPhone”(视情况而定)。请注意,有多个用户代理在运行,因此精确匹配是不明智的 - 但请查看从您的设备http://whatsmyuseragent.com

所以检查标题中的用户代理。

【讨论】:

    【解决方案4】:

    你可以通过获取 UserAgent 来做到这一点

    string ua = Request.UserAgent;
    if (ua != null && (ua.Contains("iPhone") || ua.Contains("iPad")))
    {
    ...
    ...
    ...
    }
    

    【讨论】:

      【解决方案5】:

      我会先尝试 WURFL http://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 史蒂夫桑德森在他的博客上介绍了如何做到这一点 http://blog.stevensanderson.com/2010/12/17/using-51degreesmobi-foundation-for-accurate-mobile-browser-detection-on-aspnet-mvc-3/

      51Degrees.Mobi Foundation 是一个开源 .NET 项目,它增强了 Request.Browser,因此它可以从 Wireless Universal Resource File (WURFL)(最全面和最新的移动设备信息数据库之一)获取信息。好消息是 51Degrees.Mobi Foundation 现在以 NuGet 包的形式提供,因此安装和更新非常容易。

      【讨论】:

        【解决方案6】:

        小心 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 (Mobile; 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 , 像 Gecko) Mobile Safari/537"

        【讨论】:

          【解决方案7】:

          iOS 13 用户代理更改为 Mac OS,例如:

          Mozilla/5.0(Macintosh;英特尔 Mac OS X 10_15)AppleWebKit/605.1.15 (KHTML,如 Gecko)版本/13.0 Safari/605.1.15

          【讨论】:

          【解决方案8】:
          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
          }
          

          【讨论】:

            【解决方案9】:

            您可以从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;
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-09-10
              • 1970-01-01
              • 2019-03-28
              • 2011-05-19
              • 1970-01-01
              • 1970-01-01
              • 2018-06-16
              相关资源
              最近更新 更多