Loading and displaying externally hosted SWF into an Adobe Air application - actionscript-3

I have a game written in AS3 on top of Adobe AIR. Now that I've found a publisher for the game, I need to load an external swf to display advertisements (cpmstar). Unfortunately it hasn't been straightforward, and I can't seem to get any advertisements to display on the stage. I'm wondering if I'm doing something wrong. Here's my code:
Security.allowDomain("server.cpmstar.com");
var cpmstarViewSWFUrl:String = "http://server.cpmstar.com/adviewas3.swf";
var cpmstarLoader:Loader = new Loader();
var allowSWF:LoaderContext = new LoaderContext( false, ApplicationDomain.currentDomain, null );
allowSWF.allowCodeImport = true;
var urlRequest:URLRequest = new URLRequest(cpmstarViewSWFUrl + "?contentspotid="+ < my ID > );
cpmstarLoader.load( urlRequest, allowSWF );
addChild( cpmstarLoader );
With the above, I see the url request successfully complete ( I can also see the 200 success response in Chrome ) , but I never see any content added to my stage. I've also tried, with no success, loading the distant URL with URLLoader and then reloading it through loadByte as described here: AIR Loading server hosted swf into same sandbox
If the code above looks okay, could the advertisers swf file be doing something that isn't allowing it to play nicely with this code? If so, what?
Thanks in advance.

Related

Standalone flash player navigateToURL using POST fails

Since Flash plugin has reached EOL, the only way to still use my RIA is to use the standalone version of Flash player.
I've noticed an issue with the following piece of code while testing the migration:
var request:URLRequest = new URLRequest("/utils/function");
request.contentType = "application/x-www-form-urlencoded";
request.method = URLRequestMethod.POST;
var data:URLVariables = new URLVariables();
data.x = encodeURIComponent(1);
data.y = encodeURIComponent(2);
data.z = encodeURIComponent('some value');
request.data = data;
navigateToURL(request, "_blank");
The standalone version of flash (v30.0.0.134) makes a GET request instead of the instructed POST method. The browser plugin (v32.0.0.238) opens the page correctly in a new tab as a POST request.
Why does the standalone flash convert my request in to a GET? Anybody out there who can shed some light on this issue?
I don't know if it qualifies as a answer, but we use this to make a POST request:
handleService.url='.../something.ashx';
handleService.method = URLRequestMethod.POST;
var prm:Object=new Object();
prm.par1 = "asd";
prm.content=encodedData;
prm.fileName=FileName;
handleService.send(prm);
Instead of going for a standalone version of flash, you can package the app as a Adobe AIR runtime. We still use some apps written in Flex and have no issues with them.
If running on windows, you can package it as a native runtime and works nice.

Flash sandbox not enabling me getting JSON data from server - AS3

i'm setting up a local SWF file that has to display some JSON data retrieved from a remote web server in some dynamic text fields.
Code:
Security.allowDomain("api.yourserver.com");
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://api.yourserver.com/yourendpoint");
request.method = URLRequestMethod.GET;
request.contentType = "application/json";
loader.load(request);
loader.addEventListener(Event.COMPLETE, decodeJSON);
function decodeJSON(event:Event):void{
var loader:URLLoader = URLLoader(event.target);
var Info:Object = JSON.parse(loader.data);
cont.textfield1.text = Info.text.field1;
cont.textfield2.text = Info.text.field2;
cont.textfield3.text = Info.text.field3;
}
Control > Test - It works. When run standalone it doesn't. I get the 2028 error (sandbox violation).
What i tried:
The LoaderContext method explained here on StackOverflow but i get a 2124 error (Loaded file is an unknown type - seems like the Loader method can only be used with stuff like SWF or medias like JPG etc.);
Setting local playback as described always here on StackOverflow but it didn't help;
Setting up and exception in the Global Flash Player Trust directory as explained here but got the 2028 again;
Anyone who was able to overcome this and willing to explain how or at least pointing in the right direction?
Thanks in advance!
I think that your current published file has just assess to local files only and not network ones, that's why you got security #2028 error.
To avoid that, you can change the Local playback security for your published swf from the Publish Settings to Access network only :
If you still get security errors when testing locally ( like #2048 error ), take a look on my answer of this question to add your local swf file to the trusted locations ...
Hope that can help.

Flash player stuck at Frame 1 after calling a function

I'm working in a flash CS6 and I'm having a trouble: After calling a function, player freezes at frame 1. This not happend during Ctrl+ENTER preview, but when I play the .swf file published (using flash player or opening it on a web browser, doesn't matter) is when the problem begin.
This is the code:
import flash.display.MovieClip;
var code:int = 0
var temp:int = 0;
var _xmlURL:String = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid=368335%20and%20u=%27c%27";
var _xmlData:XML;
function loadXML(xmlURL:String):void {
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest(_xmlURL);
loader.load(request);
loader.addEventListener(Event.COMPLETE, loadData);
}
function loadData(event:Event):void{
_xmlData = new XML(event.currentTarget.data);
var dataG:XMLList = _xmlData.results.channel.item.elements();
code = dataG[5].#code;
temp = dataG[5].#temp;
trace(code);
trace(temp);
}
loadXML(_xmlURL);
I'm not used to use as3, I don't know if I'm using it right.
As you can see, the code reads an external xml file using "URLLoader" and its method ".load".
Thanks for your help.
BTW, I've already tried to play the published ".swf" file in other PCs (xp, seven, 8), one of them with Windows recently installed (seven).
Most likely (because you're loading in resources from the internet, and it works when you test), this has to do with the security settings of your application.
Go to your publish settings file -> publish settings.
You'll see a drop down labelled Local playback security. Ensure this is set to access network only and not the default acess local only.
This hangs up a lot of people when they first start using flash.
It's always good practice too, to listen not just for the COMPLETE event on your loaders, but also for error events such as:
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandlerFunction);
loader.addEventListener(IOErrorEvent.NETWORK_ERROR, ioErrorHandlerFunction);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandlerFunction);
In your case, it's probably throwing a security error.

Need alternative to POST data using navigateToURL that works with Pepper Flash plugin

As most people know, Google messed several flash applications after starting using Pepper Flash plugin in Chrome.
I'm getting the same problem, but it can't be solved with external interface call, as I need to post data using POST, to a php file.
My application basicaly takes a screenshot, and post to a php file. Worked fine before, but now with Pepper Flash, nothing happens.
This is the piece of code that POSTs the data.
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = jpgStream;
navigateToURL(URL_TO_THE_PHP_FILE, "_self");
Is there a workaround for this? My website that uses this application have thousands visitors daily, and most users woudn't know how to disable pepper flash.
thanks.
To upload image, you will need URLLoader, image encoding algorithm, and some contract(names of variables in POST's body, encoding algorithm, etc) with you server-side
var urlRequest : URLRequest = new URLRequest();
var urlLoader : URLLoader = new URLLoader();
var urlVars : URLVariables = new URLVariables();
//Take from the image bytes, for example with JPEGEncoder
//Endoce ByteArray for example with Base64
//If image is big, you can split on several parts
urlVars.image = Base64.encode(imageData);
urlRequest.url = $snapshotUploadURL;
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = urlVars;
// create the image loader & send the image to the server;
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.load(urlRequest);
BloodyCrypto is very helpful library for this task.

Flash AS3 : Error #1014 : When loading remotely but not locally

Starting with a blank project, when I load a SWF which has a dependence on ISomeInterface defined in a swc which is compiled into my blank project
var lc:LoaderContext = new LoaderContext( true, ApplicationDomain.currentDomain );
var loader:Loader = new Loader();
loader.load( new URLRequest( "Some.swf"), lc );
Not too surpisingly all is good as the interface it requires is already in the application domain into which it has been loaded.
However, when I load the same file from a remote url
loader.load( new URLRequest( "http://127.0.0.1/Some.swf"), lc );
I get the evil
[Fault] exception, information=VerifyError: Error #1014: Class ISomeInterface could
not be found
What am I missing that makes these different?
My issue appears to have been 2 fold
1) When loading the asset locally, it will by default be loaded into the correct security domain. However, when loading from a web site, I need ensure that I set the security domain correctly
new LoaderContext( true, ApplicationDomain.currentDomain, SecurityDomain.currentDomain )
2) However, you can only use the SecurityDomain when the swf you are doing the loading from was actually itself loaded remotely :
Security.sandboxType == Security.REMOTE
So no mix mode of local and remote.
In the end it was a matter of simply loading the first SWF from a website, and adding the correct SecurityDomain.
In my searching, this was the best discorse I found on the topic of Security Domains and Applciation Domains http://www.senocular.com/flash/tutorials/contentdomains/
Since your testing locally you might want to check this out:
http://jansensan.net/flash-player-security-settings-to-develop-locally
Essentially go to the Flash Player security page:
http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html
Click Edit Location -> Add location
Add the project folder or your filesystem root.