4

I am a little confused on whether to embed swfs or load them when making apps for the iphone. Does anyone know what are the advantages of each (which is preferable to use)? I know that embedding swfs should be a little faster than loading them but is that all?

Also and this is kind of important, I read that Apple will reject any app with external swfs?? Is that indeed correct? If so, will embedding or loading said swfs solve this problem?

Thanks in advance

EDIT: after some searching around, turns that Apple does have problems with loading external swfs, u can still do it, but your swfs need not have any actionscript attached, here is a link http://whizzkid74.blogspot.com/2010/12/air-for-iphone-loading-external-swf.html It doesnt say anything about embedding swfs however, so my question is this: can u embed swfs when writing apps on the iphone??

EDIT To clarify, when i say external SWFs. I mean SWFs that are found on your system locally but you need to add them to your program since they contain MovieClips or Sprites etc... that u need. I didn't mean SWFs that you need to download from a website or an online source. (hope that clarifies things)

EDIT Changed the question's title...Problem solved, thanks for all the help guys and happy new year =D

r3x
  • 2,125
  • 5
  • 23
  • 39

3 Answers3

7

Update, Oct 2012:

In Adobe AIR 3.5, adl is introducing a feature called "multiple SWF support" that allows the use of Loaders to load SWFs delivered in the .ipa (local files, not from servers) to have code in them. The adl compiler AOT compiles SWFs that are included in the .ipa, allowing them to be loaded and work under iOS.

AIR 3.5 is currently in beta at the time of this writing, available on the adobe labs site:

Note that this feature requires -swf-version=18 or greater of the root SWF (not necessarily the assets being loaded) and AIR namespace ending in 3.5 in the application xml file.

Older answer:

I wanted to update this answer because I've learned a lot about this issue since I first looked into it.

The root of the issue is that, when making iOS apps with AIR, Apple TOS disallows runtime interpretation of code - and this includes SWF bytecode. So loading SWFs with code in them (even simple animation commands like stop(), gotoAndPlay(), etc) is disallowed and will not work via a Loader (prior to AIR 3.5).

  • Note that it's perfectly fine to load SWFs for their vector graphics content. The graphics will display, but the code will not execute.

However, there are a few workarounds for this. Both workarounds avoid a Loader by compiling assets with code in them into the main SWF, because once they're part of the main SWF, the AIR compiler (adt) will cross-compile the code into objective-c, and everything will work fine on iOS devices.

Using SWC libraries

This is the best option for iOS development. If you compile your graphical assets (.fla file) into SWCs (or export SWCs from symbols in your library), then compile your main swf against these SWCs, this goes through the compiler and actionscript code will execute on iOS devices.

Using SWFMerge for [embed]ed SWFs

Embedding assets into SWFs is very easy, and looks like this:

[Embed(source="GameLevel.swf")]
  private var GameLevel:Class;

public function main():void
{
  var my_level:* = new GameLevel();
  addChild(my_level);
}

In this scenario, if gameLevel.swf has code in it, it typically wouldn't work in iOS, because new gameLevel() would create a Loader and interpret SWF bytecode. But, if you first run the above SWF through my tool called SWFMerge, it will take your embedded SWF and merge it into your root SWF. Then ADT will compile your main swf (including embedded code) into objective-C, it will work on iOS, and note: new gameLevel() now results directly in an instance of your asset - NOT a Loader.

The SWFMerge tool is here:

http://www.onetacoshort.com/temp/SWFMerge_alpha.swf

Let me know in the comments if this workaround works for you or if you have trouble.

Using Loaders

Prior to AIR 3.5, if you use a Loader to load a SWF file (whether this swf is included in your IPA or served from a webserver), the target SWF graphics will load just fine, but no code inside the SWF will execute, again because this is disallowed by Apple's TOS.

As of AIR 3.5, packaging SWF files in the .ipa as assets, using a Loader will work even if they contain code as this code is now AOT-compiled by adt. This requires -swf-version=18 or greater of the root SWF (not necessarily the assets) and AIR namespace ending in 3.5 in the application xml file.

However, it is technically possible to interpret SWF bytecode, it's simply an App Store legal restriction not to. If you only want to test on an iOS device and won't be distributing your app via the App Store, you can compile your SWF using adt's -target ipa-test-interpreter option, and Loading SWFs with code in them will work.

Jeff Ward
  • 16,563
  • 6
  • 48
  • 57
  • I am using inDesign. I use it to export to FLA. Then I export to SWF. I have tried to embed and add the child directly but I get the error Error #2100: The ByteArray parameter in Loader.loadBytes() must have length greater than 0. And If I use ProLoader I get the same error, and the project have -swf-version=18 and the namespace ending in 3.5. – ccsakuweb Nov 26 '12 at 09:07
  • 1
    @ccsakuweb, if I understand correctly, you're using embed to embed a SWF, then trying to load that SWF using Loader.loadBytes? If you SWF has any code in it, that won't work for iOS as loadBytes is not supported (because it implies runtime code. [Contact me](http://www.lilcodemonkey.com/SpeedWords/contact.php) and I'll see if I can help you. – Jeff Ward Nov 26 '12 at 20:50
  • I understood that using AIR 3.5 it would work. =( Then I understood bad.. I would like to have a list of swfs, that swfs has TLF text inside. Finally I got load the swfs but the TLF text is not shown. I suposse that it is because that restriction. – ccsakuweb Nov 29 '12 at 07:51
  • I'm not sure if has swc instead of swf will work to me. Because I need a different swc (before swf) in each item of a list. > – ccsakuweb Nov 29 '12 at 08:08
  • I have used your merge and the TLF continue without shown. >< Could you help me please? http://stackoverflow.com/questions/13600859/why-tlf-text-works-in-debug-on-device-mode-but-not-in-a-release-ad-hoc – ccsakuweb Nov 29 '12 at 10:18
3

I'm not familiar with the rules around Apple and iPhone apps, but generally when I make apps/games, I embed everything. The advantages:

  • Your game/app is in one swf, making it easy to share
  • Your game/app loads quicker as all the assets are there at load-time
  • You're not dependant on internet access to get your assets (if you're loading them from a site) - good for an app
  • Your game/app will "just work" - no security issues, no problems with urls changing/asset site going down (if they're external)
  • If you use a swc instead of an swf (an swc can also be loaded btw), then you can also benefit from strict-typing

The disadvantages:

  • If you need to update one of your assets, you need to republish the game/app
divillysausages
  • 7,883
  • 3
  • 25
  • 39
  • Embedding seems they way to go for me. Thanks – r3x Jan 02 '12 at 00:46
  • 1
    _Your game/app will "just work"_ - unfortunately this isn't true on iOS. Code will not be executed in [embed]ed SWFs with code in them (due to Apple restrictions). See my answer for more details. – Jeff Ward Sep 03 '12 at 05:14
  • _If you use a swc instead of an swf (an swc can also be loaded btw), then you can also benefit from strict-typing_ What you mean? How can I add the swcs? In libs folder and import each other?? How can I do dinamically? Load a diferent swc in each item of a list is strange right? :S I don't know how I will do. With a loader will work? – ccsakuweb Nov 29 '12 at 08:06
2

The answer is yes, you can embed SWFs, but that is not the only way. You can also use normal Loader methods, with everything relative to the main SWF:

var myLoader:Loader = new Loader();
// iOS apps act as if the main SWF is in a folder 
// and the other SWFs are in the same (or sub) folders. 
var url:URLRequest = new URLRequest("loadedSWF.swf");
myLoader.load(url);
addChild(myLoader);
myLoader.x = 50;
myLoader.y = 30;

With one caveat: during compile time (in the "Air for iOS Settings", in the "Included files" list), you have to list the SWFs that you want to be loaded, and they will get compiled into the IPA.

So, no you cannot call to external SWFs, but you certainly can access other SWFs using the embed tag, or using (what might be called) IPA embedding.


Edit:

Since the question and title have changed a bit since I first answered this, here is a more generic summary. Apple does not allow loading local SWFs in the same way that you would be able to if using Flash Player on a website. There is no way to load an SWF (or anything else) using "relative" or "local" references in an i-device unless that content is compiled into the app. This does not apply to some types of "remote" SWF loading, and also does not apply to Actionscript "Native" coding, but that is not what the (original) question was about.

iND
  • 2,663
  • 1
  • 16
  • 36
  • So just to know I get things right (since crooksy said otherwise)...I can indeed embed or load local swf files and I will not get any problems with apple. For loading though, i need to include the swf files as u said BUT i cannot load external swf files from an online source (though this is not what I need, so it doesn't matter)...correct? – r3x Jan 01 '12 at 23:22
  • Right. No external (w.g., some website) SWF loading at runtime on your iDevice is a restriction from Apple; the embed/compile options become part of the Air IPA. – iND Jan 01 '12 at 23:58
  • Awesome, thanks. U helped a lot. I just noticed that my question changed from a comparison between embed and loading, to whether it is possible to embed and load swf files to your iphone. Anyways I will accept your answer and edit my original post to suit it. – r3x Jan 02 '12 at 00:35
  • "Right. No external (w.g., some website) SWF loading at runtime on your iDevice is a restriction from Apple; the embed/compile options become part of the Air IPA" This is not true. Apple only restricts external swfs which contain actionscript. – WendiKidd Aug 31 '12 at 16:39
  • @WendiKidd Please note that the question and the title were changed a few times. The answer is correct: you cannot load local SWFs in the same way that you can using Flash Player on a website. There is no way to load an SWF (or anything else) using "relative" or "local" references in an i-device *unless* that content is compiled into the app. This does not apply to some types of "remote" SWF loading, but that is not what the (original) question was about. – iND Aug 31 '12 at 21:05