Home >
Flex Quick Tip: Detecting Local vs. Remote Instance
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
If your application is loaded from a server, this will be the fully qualified url to the SWF, for example:
If your application is loaded from a local directory, this might be something more like:
Since this is a simple string value, you can user a regular expression and the string's
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
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
file:///C:/Documents%20and%20Settings/atrice/My%20Documents/Flex%20Builder%203/globe/bin-debug/StaticPapervisionGlobe.swf
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
}
Enjoy!
___________________________________
Andrew Trice
Principal Architect
Cynergy Systems
http://www.cynergysystems.com




Facebook Application Development
Excellent! Great tip.
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.
Thanks for the tip! Very useful!
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).