Loading External SWF with Packager for iPhone

Posted on November 30, 2010 | 15 comments

This is not a very technical post but one to show that it is possible to load SWFs in iOS applications. Any actionscript in the SWF will not execute, my example is just a simple animated SWF. In this code example I have one SWF with a animated vector over 20 frames. I published it and put it next to my local project to be packaged up in the ipa and also put a copy on at renaun.com to test loading remotely.

Here is the source files on github.

Here is the application test file that loads an animated SWF called ExternalAnimatedSWF.swf:

package
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.net.URLRequest;
   
    public class PFIExternalSWFTest extends Sprite
    {
        public function PFIExternalSWFTest()
        {
            init();
        }
       
        private var loaderLocal:Loader;
        private var loaderRemote:Loader;
       
        private function init():void
        {
            loaderLocal = new Loader();
            loaderLocal.load(new URLRequest("ExternalAnimatedSWF.swf"));
            loaderLocal.x = 10;
            loaderLocal.y = 10;
            addChild(loaderLocal);
           
            loaderRemote = new Loader();
            loaderRemote.load(new URLRequest("http://renaun.com/flex4/ExternalAnimatedSWF.swf"));
           
            loaderRemote.x = 60;
            loaderRemote.y = 60;
            addChild(loaderRemote);
        }
    }
}

Make sure to include the SWF file in the PFI’s command arguments to bundle it with the ipa.

  • http://whizzkid74.blogspot.com whizzkid74

    Thanks for the example.
    when I add a library symbol which has ‘export for actionscript’ enabled, the swf doesnt load at all.
    No error event is thrown either..
    is it supposed to work that way? i.e. you can load a swf file, but only as an animation, you can’t use it to store assets in ?

    regards,

    • http://www.renaun.com Renaun Erickson

      I just confirmed that if you have a library symbol that is set to export for actionscript it does not work.

      • http://whizzkid74.blogspot.com whizzkid74

        Found another way to screw up swf loading.

        Give your instance on stage a name. and the swf won’t load anymore..

        So basically you CAN load swf’s, but only if they contain 100% static timeline animations.

        Thats something worth mentioning in the docs methinks..

        • http://www.renaun.com Renaun Erickson

          It works for me with instance names, might be something else in the SWF

          • http://whizzkid74.blogspot.com whizzkid74

            Ah, yes, it works.
            Don’t know what went wrong.
            An swf that didnt load, now loads again.

            Ok, now we’ve got something useful afterall, because you can still reach the named instances on stage, and move them to another displayobjectcontainer or do whatever with em.
            So with a bit of as3 and gotoAndStop magic, you can still use an external swf as a library on the iPhone..

  • http://whizzkid74.blogspot.com whizzkid74

    I’ve created another example and a class that helps you use the loaded .swf for iPhone, (also works on android) on my blog

    http://whizzkid74.blogspot.com/2010/12/air-for-iphone-loading-external-swf.html

    (GotoAndStop() didn’t quite cut it, because it wrecks your animations partially..)

  • Pulkit Gupta

    Hi Renaun

    Is a SWF with static text and graphics considered to be a scripted SWF by AIR on iOS. Pro’ly because it exposes the TextSnapshot object? I had an SWF with just an image, even that didn’t work till I broke it apart. Can you throw some light on What kind of SWFs are supported?

    Flex for iOS is a bit strange…I kept debugging on the device by compiling with fast mode and it did load the SWFs and once I create a release build, stops working.

    Please throw some light on this.

  • Pulkit Gupta

    Hi Renaun

    Is a SWF with static text and graphics considered to be a scripted SWF by AIR on iOS. Pro’ly because it exposes the TextSnapshot object? I had an SWF with just an image, even that didn’t work till I broke it apart. Can you throw some light on What kind of SWFs are supported?

    Flex for iOS is a bit strange…I kept debugging on the device by compiling with fast mode and it did load the SWFs and once I create a release build, stops working.

    Please throw some light on this.

    • Anonymous

      There is no easy way to figure this out. To many times Flash Pro or some SWF will have actionscript in there you are not seeing. Fast packaging mode is an interpreted mode so its not a good test for this particular issue. Best approach is to tweak the SWFs until it works for you. But generally you should stay away from it if possible and just use images.

      • Pulkit Gupta

        Thanks for the timely reply. I find it a bit strange because the whether a SWF is valid or not is decided by AIR and iOS platform has no role in it. So, it should be absolutely clear what can be used and what not?

        We have a PDF reader product and we convert each page of PDF as a SWF to be used by our app. The product is already live on desktop, now we are working to bring it to mobiles. 2 key reasons of using SWF instead of images are a) Ability to highlight searched words b) It’s vector so does not distort on scaling. Can’t tweak SWFs manually much as they are auto generated.

        Just to let you know we researched further and found that SWFs with images and text are all actually working fine and the textSnapshot is able to highlight words. Strangely a SWF with just image in it was not working and worked on breaking the image apart…. So as you mentioned, no conclusion on what works and what not. But I hope, I am able to reach a conclusion as I really want to use SWFs.

        Thanks and I really appreciate yours being so actively posting any updates at Flex / AIR’s end.

      • Pulkit Gupta

        Thanks for the timely reply. I find it a bit strange because the whether a SWF is valid or not is decided by AIR and iOS platform has no role in it. So, it should be absolutely clear what can be used and what not?

        We have a PDF reader product and we convert each page of PDF as a SWF to be used by our app. The product is already live on desktop, now we are working to bring it to mobiles. 2 key reasons of using SWF instead of images are a) Ability to highlight searched words b) It’s vector so does not distort on scaling. Can’t tweak SWFs manually much as they are auto generated.

        Just to let you know we researched further and found that SWFs with images and text are all actually working fine and the textSnapshot is able to highlight words. Strangely a SWF with just image in it was not working and worked on breaking the image apart…. So as you mentioned, no conclusion on what works and what not. But I hope, I am able to reach a conclusion as I really want to use SWFs.

        Thanks and I really appreciate yours being so actively posting any updates at Flex / AIR’s end.

      • Pulkit Gupta

        Just to let you know, I researched a bit further and found that the issue was with the actionScript version of the SWF.

        Even though the SWF had no script, just because it was compiled as an AS2 SWF, it was rejected by AIR on iOS. De-compiled the same, and published as AS3, it now works perfectly. Thanks for your timely reply.

      • Pulkit Gupta

        Just to let you know, I researched a bit further and found that the issue was with the actionScript version of the SWF.

        Even though the SWF had no script, just because it was compiled as an AS2 SWF, it was rejected by AIR on iOS. De-compiled the same, and published as AS3, it now works perfectly. Thanks for your timely reply.

    • Anonymous

      There is no easy way to figure this out. To many times Flash Pro or some SWF will have actionscript in there you are not seeing. Fast packaging mode is an interpreted mode so its not a good test for this particular issue. Best approach is to tweak the SWFs until it works for you. But generally you should stay away from it if possible and just use images.

  • Mike

    We are utilizing frame labels to allow the shell to control children in the loaded swf.

    Mike
    @impactbros