Search code examples
actionscript-3apache-flexairflash-builderstagewebviewbridge

how do I add a stagewebviewbridge instance to stage with FB 4.6


Im using the following code in flash builder 4.6 (as a test mobile app for android and ios, trying to replace stagewebview with stagewebviewbridge to take advantage of the javascript communication benefits)...

The code comes directly from the documentation and seems straight forward, but the webview instance doesnt display and it seems that I can only get as far as the trace where "END_DISK_PARSING" shows in the console...

the onDeviceReady function is never called by the Event listener for DEVICE_READY for some reason...

It doesnt produce any error and the console only shows "End_Disk_Parsing". Im using FB 4.6 premium, flex sdk ver 4.12 and am trying to test on an android device (HTC One) and have permission to internet specified in the -app.xml file.

Anyone familar with SWVBridge and know what I should do differently here?

Ive used the default stagewebview tons of times and am very familiar with how that works but cant get any on stage visible results with stagewebviewbridge so far...

code Im using follows:

import es.xperiments.media.StageWebViewDisk;
        import es.xperiments.media.StageWebviewDiskEvent;
        import es.xperiments.media.StageWebViewBridge;
        import es.xperiments.media.StageWebViewBridgeEvent;
        import flash.events.Event;
        import flash.events.MouseEvent;

        // this is our main view
        public var view:StageWebViewBridge;

            public function init1():void{
                // init the disk filesystem
                StageWebViewDisk.addEventListener( StageWebviewDiskEvent.END_DISK_PARSING, onInit );
                StageWebViewDisk.setDebugMode( true );
                StageWebViewDisk.initialize( stage );
            }

            // Fired when StageWebviewDiskEvent cache process finish 
            public function onInit( e:StageWebviewDiskEvent ):void{
                trace( 'END_DISK_PARSING');    

                // create the view
                view = new StageWebViewBridge( 0,0, 320,240 );

                // listen StageWebViewBridgeEvent.DEVICE_READY event to be sure the communication is ok
                view.addEventListener(StageWebViewBridgeEvent.DEVICE_READY, onDeviceReady );

                // load a test url...
                view.loadURL('http://www.google.com');

            }

            public function onDeviceReady( e:Event ):void{
                trace('onDeviceReady\n');
                // all is loaded and ok, show the view
                addChild( view );
            }

Solution

  • Solved this after a fresh night of sleep and a cup of coffee:

    This code works fine in for me :

    import es.xperiments.media.StageWebViewBridge;
                        import es.xperiments.media.StageWebViewBridgeEvent;
                        import es.xperiments.media.StageWebViewDisk;
                        import es.xperiments.media.StageWebviewDiskEvent;
                        import mx.core.UIComponent;
                        import flash.media.StageWebView;
    
                        import spark.events.ViewNavigatorEvent;
    
                        protected var webView:StageWebViewBridge;
    
                        protected function view1_addedToStageHandler(event:Event):void
                        {
                                // OPTIONAL BEFORE INIT OPTIONS SETTING
                                StageWebViewDisk.setDebugMode( true );
                                // if we need debug mode assign it before initializaton
                                // StageWebViewDisk Events. First time app launches it proceses the filesystem
                                // As it can take some time, depending of the filesize of the included files
                                // we provide 2 events to know when process start/finish
    
                                StageWebViewDisk.addEventListener(StageWebviewDiskEvent.START_DISK_PARSING, onDiskCacheStart);
                                StageWebViewDisk.addEventListener(StageWebviewDiskEvent.END_DISK_PARSING, onDiskCacheEnd);
                                StageWebViewDisk.initialize( this.stage );
                        }
    
                        protected function onDiskCacheStart(e:StageWebviewDiskEvent):void
                        {
                                trace("SWVD parsing started");
                        }
    
                        protected function onDiskCacheEnd(e:StageWebviewDiskEvent):void {
                                trace("SWVD parsing ended");
                                webView = new StageWebViewBridge(0, 50, 400, 450);
                                webView.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
                                SWVB.addChild(webView);
                        }
    
                        protected function onAddedToStage(e:Event):void
                        {
                                webView.loadURL("http://www.google.com");
                                //webView.loadLocalURL("applink:/index.html");
                        }
    

    It also reminded me how to deal with 'add child' errors in flash builder / as3 projects... first I have to add a 'ui component' to the stage...

    // container ( IVisualElement ) for DisplayObjects
    var container:UIComponent = new UIComponent();
    addElement( container );
    

    then we can add child to that uicomponent:

    container.addChild(webView);
    

    Hope this helps someone else.