Skip to main content
Quentin T
Participating Frequently
April 22, 2011
Question

Loading local content in StageWebView (file URI scheme) not working, bug?

  • April 22, 2011
  • 2 replies
  • 6556 views

Hey there!

I'm trying to load some local content inside a StageWebView but it looks like it's not working. Here are the docs about this feature.

I think I'm doing it the way I should but I may be wrong... Someone knows how?

Here's my code:

package {      import flash.display.Sprite;      import flash.display.StageAlign;      import flash.display.StageScaleMode;      import flash.events.ErrorEvent;      import flash.events.Event;      import flash.events.LocationChangeEvent;      import flash.events.StageOrientationEvent;      import flash.filesystem.File;      import flash.geom.Rectangle;      import flash.media.StageWebView;      import flash.net.URLRequest;           import net.tw.util.air.DownloadHelper;           public class MobileSWFDownloader extends Sprite {           protected var _downloader:DownloadHelper;           protected var _swv:StageWebView;                     public function MobileSWFDownloader() {                super();                               stage.align = StageAlign.TOP_LEFT;                stage.scaleMode = StageScaleMode.NO_SCALE;                               _downloader=new DownloadHelper(new URLRequest('http://toki-woki.net/lab/eM/test.html'), File.applicationStorageDirectory.resolvePath('local.html'));                _downloader.stream.addEventListener(Event.COMPLETE, onDownloaded);                _downloader.start();           }           protected function onDownloaded(e:Event):void {                _swv=new StageWebView();                _swv.addEventListener(ErrorEvent.ERROR, trace);                _swv.addEventListener(LocationChangeEvent.LOCATION_CHANGE, trace);                _swv.stage=stage;                _swv.loadURL('file://'+_downloader.destination.nativePath);                placeWebView();                stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, placeWebView);           }           protected function placeWebView(e:Event=null):void {                _swv.viewPort=new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);           }      } }

And here's the output:

[SWF] MobileSWFDownloader.swf - 4,161 bytes after decompression [LocationChangeEvent type="locationChange" bubbles=false cancelable=false eventPhase=2 location="file:///data/data/air.fr.edumedia.MobileSWFDownloader.debug/fr.edumedia.MobileSWFDownloader.debug/Local Store/local.html"]

Problem is: the StageWebView remains blank and doesn't seem to load anything...

Any idea why?

The DownloadHelper class is a simple utility that downloads a remote file and stores it locally, available here.

Thanks!

This topic has been closed for replies.

2 replies

Inspiring
November 2, 2011

Simple method by Mike Chambers here:

http://www.mikechambers.com/blog/2008/11/06/getting-the-file-uri-of-a-file-in-an-air-apps-install-directory/

In my code it looks like this and works:

webView.stage = this.stage;

                                        webView.viewPort = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight );

                                        webView.addEventListener(Event.COMPLETE, WebViewComplete);

                                        webView.addEventListener(ErrorEvent.ERROR, WebViewError);

var fPath:String = new File(new File("app:/assets/pdfs/Booklet.pdf").nativePath).url;

webView.loadURL( fPath);

Hope that helps someone

Participating Frequently
April 26, 2011

You can push html content into the StageWebView by setting it's content, but you can't load local files. You'll get an error on the device. Download your content like you are doing and then just set it, don't set the location to something local.

Quentin T
Quentin TAuthor
Participating Frequently
April 26, 2011

Hi and thanks for your feedback,

Are saying the docs are wrong when you say "you can't load local files"?

If you follow the link to the docs (http://goo.gl/MFdZu) you'll notice it states "Load local resource" is possible with the "file:" URI scheme which is exactly what I'm trying to do!

My goal is to download a file locally and then load it into the StageWebView. I could do what you suggest, but that'd prevent me from accessing the content when offline (no Internet connection) and that'd require the users to download the file everytime they run the app...

Does that make sense?

Got any tip?

Participating Frequently
April 26, 2011

It does make sense. You could load and store the html content locally and then just set the content.

I agree that the docs say that, but I've had issues with it also. I know that everything is in beta now and we may just be dealing with an AIR bug that will disappear. If that is the case just follow the motto "whatever works".