Quantcast
Channel: yourpalmark » Flash
Viewing all articles
Browse latest Browse all 10

Updated Solution: AS3 Security Error #2122 with 300 Redirects

$
0
0

Quite a while ago Steven Sacks posted a solution to 300 redirects when loading images in Flash.
This seemed to work most of the time but the solution does not work every time (at least anymore).
One thing I noticed with Facebook in particular is that the contentLoaderInfo.url (the redirected url) was not a complete url with profile images. So loading that url would not work.

Started looking into other possible solutions and low-and-behold, the language reference actually tells you what to do.
At the bottom of the checkPolicyFile section, Adobe mentions steps to take with redirects:

  1. Examine the value of LoaderInfo.url after you have received a ProgressEvent.PROGRESS or Event.COMPLETE event, which tells you the object’s final URL.
  2. Call the Security.loadPolicyFile() method with a policy file URL based on the object’s final URL.
  3. Poll the value of LoaderInfo.childAllowsParent until it becomes true.

I put together some example source code below to walk you through the steps.

private var loader:Loader;
private var accessChildTimer:Timer;
private var url:String;

private function init():void
{
	accessChildTimer = new Timer( 100, 100 );
	accessChildTimer.addEventListener( TimerEvent.TIMER, onAccessChildTimer );
}

public function loadImage():void
{
	loader = new Loader();
	loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoaderComplete );

	var context:LoaderContext = new LoaderContext();
	context.checkPolicyFile = true;

	var request:URLRequest = new URLRequest( url );
	loader.load( request, context );
}

protected function onLoaderComplete( event:Event ):void
{
	var loaderInfo:LoaderInfo = loader.contentLoaderInfo;
	if( loaderInfo.childAllowsParent )
	{
		displayImage();
	}
	else if( loaderInfo.url != this.url )
	{
		var redirectURL:String = loaderInfo.url + "crossdomain.xml"; //Might want to write a method to append a '/' if necessary.
		Security.loadPolicyFile( redirectURL );
		accessChildTimer.reset();
		accessChildTimer.start();
	}
}

protected function onAccessChildTimer( event:TimerEvent ):void
{
	var loaderInfo:LoaderInfo = loader.contentLoaderInfo;
	if( loaderInfo.childAllowsParent )
	{
		accessChildTimer.stop();
		displayImage();
	}
}

private function displayImage():void
{
	//Access BitmapData here
}

Hopefully that helps you out!


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images