【Flex mobile】StageWebViewでローカルにあるhtmlを表示

xmlns:s="library://ns.adobe.com/flex/spark"

actionBarVisible="false" creationComplete="view1_creationCompleteHandler(event)" title="Map">

import mx.events.FlexEvent;

protected function view1_creationCompleteHandler(event:FlexEvent):void

{

if (StageWebView.isSupported)

{

var webView:StageWebView = new StageWebView();

webView.stage = systemManager.stage;

webView.viewPort = new Rectangle( 0, 0, this.width, this.height);

var htmlFilePath:String = "/assets/test.html";

var os:String = flash.system.Capabilities.os;

if(os.indexOf("Mac OS") != -1 ){//シミュレータ上では動かしているOSの環境となる。windows環境でのチェックはしていないので注意//

//シミュレーター上ではこれで動くが実機だと動かない

var path:String = new File(new File("app:" + htmlFilePath).nativePath).url;

webView.loadURL(path);

}else if(os.indexOf("iPhone") != -1){

//iphone//

var descriptor:XML = NativeApplication.nativeApplication.applicationDescriptor;

var ns:Namespace = descriptor.namespaceDeclarations()[0];

var appFileName:String= descriptor.ns::filename;

var appPath:String = File.applicationStorageDirectory.nativePath.replace(/Library.*/,"") + appFileName + ".app"

var htmlPath:String = appPath + htmlFilePath;

webView.loadURL("file:/" + htmlPath);

}else{

//android

//実機ではなぜかlinuxと返ってくるので最後に・・

var path2:String = "file:///android_asset" + htmlFilePath;

webView.loadURL(path2);

}

}

}

]]>

と言う形で、プラットフォーム毎に分ける必要がありました。

(※実機で表示されることを確認済み)

※上記は、flexモバイルプロジェクト新規作成→タブ付きアプリケーションを作っていった際の、1つのviewとして作った物です。

また、プロジェクトの方に、ローカルで表示されるhtmlは、

Assets

のような位置に追加しました。

(androidの方が、"android_asset"という固定の値から見に行かないといけなかったりで、上記がどのようにファイルを置いた場合なのか解りづらいと思ったので)