rauhryan
7/15/2010 - 4:26 PM

gistfile1.cs

 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;
        }
    }