Android - webview - shouldOverrideUrlLoading not called - when user clicks links inside iframe

Srinivasan picture Srinivasan · May 8, 2013 · Viewed 9.8k times · Source

In our app, we are using webview to load external link inside a view. Loaded the link using loadurl method and set webview client to show preloader and webchrome client to listen for any link change for the webview.

URL loaded inside webview. When link is loading, there are couple of iframes and inside it we have a text link. Onclick of the link, page need to be opened in default browser and not inside webview itself.

Using min-sdk / API level 8 to build the project. Below is the code snippet for reference. Oncreate attached webview client and webchromeclient to the webview and then assigning URL to it.

private class SomWebViewClient extends WebChromeClient{
    public void onProgressChanged (WebView view, int newProgress){
        if(newProgress == 100){

        }
    }
}
private class SomWebViewDefClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
    @Override
    public void onLoadResource (WebView view, String url){
        if(url.equalsIgnoreCase("http://google.com")){
            view.stopLoading(); 
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            view.canGoBack();
        }
    }
}

I tried adding WebViewClient to the webview and identify URL change in shouldOverrideUrlLoading method. But no event captured here. Is there any way to handle clicks inside iframe of the webview url.

When i tried different link to open in webview (which doesn't have any iframes), able to trace click triggered inside shouldOverrideUrlLoading. But when URL contains iframes and when user cliks on any link inside webview, not able to trace inside shouldOverrideUrlLoading and opened in same webview itself instead of new browser window.

When i searched for the issue iam not able to find the exact problem. If anyone faced similar issue and found solution, please share.

If anything else needed, please let me know.

Thanks in Advance, Regards Srinivasan.C

Answer

gunar picture gunar · May 24, 2013

Maybe you broke the Same Origin Policy. Have a look at this similar post.

Edit: There is a way to overcome this security policy, but you need to accept the risks that you face. In the response, make sure you have the following header: "Access-Control-Allow-Origin:"