Quantcast
Channel: aspx Tutorial - iPhone
Viewing all articles
Browse latest Browse all 2

How Can I Detect the iPhone User Agent using ASP.NET

$
0
0

IPhone application are growing in the market. In this example, I will give you a simple tips that how to detect request is coming from iPhone in ASP.NET application. When ASP.NET page is requested at runtime, the information in the request header to determine what type of browser has made the request by the user agent. In this example, you will see that if the request will come from iPhone then we can do what we have required for our application and if request will not from iPhone then we don’t need to do anything.

Here’s an example

C#

    protectedbool isAgent()

    {

        if (HttpContext.Current.Request.UserAgent.ToLower().Contains("iphone"))

            returntrue;

        returnfalse;

    }

 

    protectedvoid Page_Load(object sender, EventArgs e)

    {

        if (isAgent())

        {

            Response.Write("iPhone Detected");// what ever you do

            // you can also redirect to m.domain.com

        }

    }

 

VB.NET

    ProtectedFunction isAgent() AsBoolean

        IfHttpContext.Current.Request.UserAgent.ToLower().Contains("iphone") Then

            ReturnTrue

        EndIf

        ReturnFalse

    EndFunction

 

 

   ProtectedSub Page_Load(ByVal sender AsObject, ByVal e AsEventArgs)

        If isAgent() Then

            ' what ever you do

            ' you can also redirect to m.domain.com

            Response.Write("iPhone Detected")

        EndIf

    EndSub

 

 

Note: If you have alternative solution, please comments here to share with us.

Output

Download

Detect-Iphone- Agent.zip (930.00 bytes)

See Live demo


Viewing all articles
Browse latest Browse all 2

Trending Articles