Created
July 15, 2010 16:26
-
-
Save rauhryan/477178 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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