Home  >  

Flex Quick Tip: Detecting Local vs. Remote Instance

Author photo
February 10, 2009 | | Comments (4)
AddThis Social Bookmark Button
Have you ever had the need to manage multiple data sources in your application? Perhaps in development you want pull from one data source, and when you are running on a hosted server, pull from another data source. Well, here is an extremely basic, yet useful tip for doing exactly that.

The Application.application.url property is a string value, available to every class in every Flex application, which contains the URL of the currently loaded SWF application. Note, this is the location of the SWF file, not the HTML page that contains the SWF file.

If your application is loaded from a server, this will be the fully qualified url to the SWF, for example:
http://www.tricedesigns.com/portfolio/globe/StaticPapervisionGlobe.swf
If your application is loaded from a local directory, this might be something more like:
file:///C:/Documents%20and%20Settings/atrice/My%20Documents/Flex%20Builder%203/globe/bin-debug/StaticPapervisionGlobe.swf
Since this is a simple string value, you can user a regular expression and the string's search() function to find the value "http:" or "https:" in the url. If you find it at position 0 (the first character in the string), you know you're on a web server, otherwise it is safe to assume that you're on the local file system. The regular expression /https*:/ will match both "http:" and "https:", so it will cover you regardless of whether or not you are on a secure server.

if ( Application.application.url.search( /https*:/ ) == 0 )
{
   //handle logic for the remote server instance
}
else
{
   //handle logic for the local file instance
}
Yes, it's that easy. However, I would strongly recommend to only use this approach during development, not in a production build. You wouldn't want someone who copies your swf to their local file system to have a different experience, or even worse, circumvent authentication!

Enjoy!

___________________________________
Andrew Trice
Principal Architect
Cynergy Systems
http://www.cynergysystems.com


Read more from Andrew Trice. Andrew Trice's Atom feed

Comments

4 Comments

Joseph said:

Excellent! Great tip.

Michael VanDaniker said:

I wrote a little utility class that checks if you're running an swf in debug or release mode. The majority of the time this is the same problem as determining if you're running locally or remotely.

The technique I use is to throw an error and look at the stack trace. The stack trace will contain line number information when you're running a debug build in a debug player. Under any other circumstances, it won't.

This method is actually safe to use in production. A user couldn't take advantage of a locally stored swf because they would only be able to download a release build.

Dan Cashman said:

Thanks for the tip! Very useful!

Chris Luebcke said:

It's a great technique and one I've used before. However, I do find that for significant apps I'll often end up creating an XML configuration file, loaded sometime prior to ApplicationComplete, that contains service URLs as well as other configuration options (for example, running locally I might point to a development or staging database, or on a dev server I might switch between mock and live services; none of those decisions can be made conclusively just by looking at the Flex app's URL).

Leave a comment


Tag Cloud

iPad

What's your take on the iPad? (Putting aside the Flash/iPad flame war)

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.