Skip to content

Instantly share code, notes, and snippets.

@rauhryan
Created July 15, 2010 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rauhryan/477178 to your computer and use it in GitHub Desktop.
Save rauhryan/477178 to your computer and use it in GitHub Desktop.
public class MobileViewBuilder : StructureMapWebFormsControlBuilder
{
private readonly HttpRequestWrapper _requestWrapper;
public MobileViewBuilder(IContainer container, HttpRequestWrapper requestWrapper)
: base(container)
{
_requestWrapper = requestWrapper;
}
public override Control LoadControlFromVirtualPath(string virtualPath, Type mustImplementInterfaceType)
{
if (IsMobileSafari(_requestWrapper))
{
string physicalPath = UrlContext.ToPhysicalPath(virtualPath.Replace(".aspx", "_iphone.aspx"));
if (File.Exists(physicalPath))
virtualPath = virtualPath.Replace(".aspx", "_iphone.aspx");
}
return base.LoadControlFromVirtualPath(virtualPath, mustImplementInterfaceType);
}
public bool IsMobileSafari(HttpRequestWrapper request)
{
/* NOTE: uncomment to always show the mobile version
* I've found this useful while working in a window environment
* because aren't very many iPhone simulators for windows
* plus firebug is essential for me even though FireFox it not a WebKit browser
*/
//return true;
bool isMobileSafari = false;
if (request != null)
{
string userAgent = request.UserAgent;
if (!string.IsNullOrEmpty(userAgent))
{
int ipodIndex = userAgent.IndexOf("iPod");
int iphoneIndex = userAgent.IndexOf("iPhone");
if (iphoneIndex + ipodIndex > -1) isMobileSafari = true;
}
}
return isMobileSafari;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment