views:

333

answers:

2

How do you check if the user browsing my website is using an iPhone, and then redirect the user to another URL?

+4  A: 

Like this:

if(Request.UserAgent.IndexOf("iPhone") > 0)
    Response.Redirect("~/iPhone/");

The iPhone's user-agent is

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) 
AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3
SLaks
arg beat me to it lol. +1
KP
Why iPhone > 0? Isn't it iPhone = true?
Fogh
@Fogh: `IndexOf` returns the index of the string. You could also write `if (Request.UserAgent.Contains("iPhone")`.
SLaks
Oh ofcause, thanks!
Fogh
A: 

Check the Request.UserAgent variable for the string iPhone. The entire string would look something like this:

HTTP_USER_AGENT=Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) 
AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C25 Safari/419.3
klausbyskov
You mis-capitalized the string.
SLaks
@SLaks, thanks, I've fixed it.
klausbyskov