如何检查useragent是ipad还是iphone?

时间:2011-03-01 13:59:01

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

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

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

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

9 个答案:

答案 0 :(得分:71)

<强>更新: 由于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,与Gecko一样)Version / 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,与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;"))
{
    // iPad
}
else
{
    // Think Different ;)
}

答案 1 :(得分:20)

对于iPad user agent类似于:

  

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

对于iPhone而言,它的作用如下:

  

Mozilla / 5.0(iPhone; U; CPU,如Mac OS X; en)AppleWebKit / 420 +(KHTML,类似Gecko)版本/ 3.0 Mobile / 1A543a Safari / 419.3

根据版本而言,还有更多的iPhone 3或4

如此好,只需按照另一个答案的建议对iPhone和iPad 进行子字符串搜索

答案 2 :(得分:5)

这些设备的用户代理包括“iPod”,“iPad”或“iPhone”。请注意,有几个用户代理正在播放中,因此完全匹配是不明智的 - 但在http://whatsmyuseragent.com

查看来自您的设备

请检查标题中的用户代理。

答案 3 :(得分:4)

你可以通过获取UserAgent

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

答案 4 :(得分:4)

我会先尝试WURFL http://wurfl.sourceforge.net/

他们有.NET API和非常好的代码示例。 http://wurfl.sourceforge.net/dotnet_index.php

将帮助您的课程称为 WURFLManager ,并具有以下方法:

enter image description here

使用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)获取信息 - {{3}} - 最全面和最新的移动设备信息数据库之一。好消息是,51Degrees.Mobi Foundation现在可以作为NuGet包使用,因此安装和更新非常容易。

答案 5 :(得分:3)

小心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;诺基亚; Lumia 735)”,如iPhone OS 7_0_3 Mac OS X AppleWebKit / 537(KHTML) ,像Gecko)Mobile Safari / 537“

答案 6 :(得分:0)

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
}

答案 7 :(得分:0)

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

答案 8 :(得分:0)

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

  

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

相关问题