I want to use StageWebView instead of opening browser iOS

720 Views Asked by At

I have a textfield that loads dynamic text from a random node in a xml. Some of these nodes contains hyperlinks.

Below is the code i'm using to setup and load StageWebView and go to the link, if the text i tap has a url

import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.media.StageWebView;
import flash.geom.Rectangle;

stage.align = StageAlign.TOP_LEFT;

stage.scaleMode = StageScaleMode.NO_SCALE;

var webView:StageWebView;

var swvRect:Rectangle;

var swvHeight:Number;

var swvY:Number;

var linkURL:String;

storyTxt.addEventListener(MouseEvent.CLICK, linkClicked);

function linkClicked(e:MouseEvent):void {

var idx:int = e.target.getCharIndexAtPoint(e.localX, e.localY);
trace("Tapped:",idx);

var tf:TextFormat = e.target.getTextFormat(idx);
    if(tf.url != "" && tf.url != null) {
        linkURL = tf.url;
        trace(linkURL);

        if(webView!=null){

            return;
        }

         webView=new StageWebView();

         webView.stage=this.stage;

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

         webView.addEventListener(ErrorEvent.ERROR,onError);

         webView.addEventListener(LocationChangeEvent.LOCATION_CHANGING,onChanging);

         webView.addEventListener(Event.COMPLETE,onComplete);

         webView.loadURL(linkURL);

    }
}

When i tap on the text that has a link, it opens both the mobile's browser [Safari] and the StageWebView. Is there a way of stopping the app switching over to the browser to open the link?

Thanks Daniel

2

There are 2 best solutions below

0
On

You could try something like:

function onChanging(event:LocationChangeEvent):void
{
    //prevents the loading of the new URL
    event.preventDefault();
    //open safari/external browser
    navigateToURL(new URLRequest(event.location));
}
0
On

I don't have a URLRequest/URLLoader function in my code so where can i place e.preventDefault()?

I think it's because of the anchor tag from the xml text that is opening the browser when i tap on the hyperlink text.