Fred Ross-perry
Fred Ross-perry

Reputation: 71

StageWebView, iOS, local files

I'm building a mobile app for IOS using Flash Builder, Flex 4.6 and AIR 3.5. I'm investigating using StageWebView to render some HTML5 content.

What I want to do is to build the content into the app, as opposed to putting it on a server. It's relatively static. But I read (and confirmed) that in-app files can't be used directly by StageWebView. But following a suggestion, I'm have the app copy the content to a temp folder, then create a file:// URL for StageWebView, which seems to work:

//  create the view
var webView:StageWebView = new StageWebView();
webView.stage = this.stage;
webView.viewPort = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight);

//  copy the HTML5 content to a temp directory
var src:File = File.applicationDirectory.resolvePath("myFolder");
var temp:File = File.createTempDirectory().resolvePath("myFolder");
copyFolderContentsToAnotherFolder(src,temp);

//  what's the URL
var newPath:String = "file://" + temp.nativePath + "/index.html";

//  load it
webView.loadURL(newPath);

Is this a bad idea? Will temporary files pile up in my device with no way to delete them?

I also thought of having the app implement an minimal HTTP server by listening on a port and supplying the data for requested files as they come. This would allow us to serve the files to StageWebView from their in-app locations, without copying. We do this in a desktop air app and it works very nicely. But, that approach uses ServerSocket, which I discover is not supported on mobile. Is there an alternative way to do this?

Finally, StageWebView does not work well in the Flash Builder iOS simulator, making debugging difficult. Is it best to just go and get FB 4.7, which (should) allow me to use it with XCode's iOS simulator?

thanks

Upvotes: 1

Views: 2826

Answers (5)

Deyan Vitanov
Deyan Vitanov

Reputation: 784

After Adobe updated the SDK - the @Jesse Crosen method does not work for android but only on iOS. To make it work on Android you have to copy the packaged file like this:

var htmlFile:File = File.applicationDirectory.resolvePath("html/index.html");  
var fileDest:File = File.applicationStorageDirectory.resolvePath("html/index.html"); 
htmlFile.copyTo(fileDest, true);
webView.loadURL("file://" + htmlFile.nativePath);

Upvotes: 0

Jesse Crossen
Jesse Crossen

Reputation: 6995

For anyone looking for a current solution to this problem that's simpler than writing an HTTP server, this is what worked for me (on iOS only):

webView.loadURL(new File(new File("app:/myFolder/index.html").nativePath).url);

But there is a caveat, which is that it seems you can't pass in a query string or hash at the end of the URL. Data can be passed to the HTML content by loading a javascript: URL once the main content is loaded.

There's a page with broader information covering iOS and Android here.

Upvotes: -1

Fred Ross-perry
Fred Ross-perry

Reputation: 71

It seems that StageWebView, when used with iOS, works best with remote files. So, I am using some Actionscript code that implements a simple HTTP server, serving up local files. Then, I give StageWebView http urls that use localhost, and a port number. Works swell.

Upvotes: 0

Fred Ross-Perry
Fred Ross-Perry

Reputation: 435

It's been too long since I thought about this. But as I recall I was able to use in app content without making copies, for iOS. But I am reading today where this might not be possible on Android.

Upvotes: 0

Fred Ross-perry
Fred Ross-perry

Reputation: 71

Well, it seems I CAN directly address the in-app content, I just have to construct a file:// URL for it:

var src:File = File.applicationDirectory.resolvePath("myFolder/index.html");
var newPath:String = "file://" + src.nativePath;
webView.loadURL(newPath);

Upvotes: 3

Related Questions