How do I check if the useragent is an ipad or iphone?

前端 未结 9 2500
礼貌的吻别
礼貌的吻别 2020-12-09 14:42

I\'m using a C# asp.net website.

How can I check if the user using ipad or iphone? How can I check the platform?

For example, if the user enter the websi

相关标签:
9条回答
  • 2020-12-09 15:35

    I would try WURFL first http://wurfl.sourceforge.net/

    They have .NET API and Very good code samples. http://wurfl.sourceforge.net/dotnet_index.php

    The class that will help you is called WURFLManager and has the following methods:

    enter image description here

    Use WURFL http://wurfl.sourceforge.net/dotnet_index.php

    If you using asp.net mvc you can use an 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);
            }
        }
    }
    

    There is also 51Degrees.Mobi Steve Sanderson has covered how to do this on his blog http://blog.stevensanderson.com/2010/12/17/using-51degreesmobi-foundation-for-accurate-mobile-browser-detection-on-aspnet-mvc-3/

    51Degrees.Mobi Foundation is an open source .NET project that enhances Request.Browser so it gets its information from Wireless Universal Resource File (WURFL) – one of the most comprehensive and up-to-date databases of mobile device information. The great news is that 51Degrees.Mobi Foundation is now available as a NuGet package, so it’s incredibly easy to install and update.

    0 讨论(0)
  • 2020-12-09 15:36

    Be careful of Windows phones! For some weird reason lots of Windows phones say "like iPhone" in the user agent. So you want to check:

    public bool IsIPhone
    {
        get
        {
            if (!UserAgent.ToUpper().Contains("LIKE IPHONE"))
            {
                return UserAgent.ToUpper().Contains("IPHONE");
            }
            return false;
        }
    }
    

    Example Windows phone user agent (from 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, like Gecko) Mobile Safari/537"

    0 讨论(0)
  • 2020-12-09 15:43

    you can get client OS data from Request.UserAgent get OS name and OS version.

      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;
        }
    
    0 讨论(0)
提交回复
热议问题