Android webView shouldOverrideUrlLoading for ifram

2019-04-01 00:46发布

I have a local website that is Javascript controlled and am loading it in a WebView. The website is really one main page with an iframe whose content changes based on user input. There is a "Next" button on the main page that runs some javascript functions and decides which page to load in the iframe (via document.getElement(...).src = )

In Android 2.1 things were working fine - simulated and on phone. The webview was loading all the pages properly. I tested it on a 2.2 phone and whenever I click Next the page (that should be loaded in the iframe) is loaded in the default browser.

I defined an intent filter

   <intent-filter>
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />
     <category android:name="android.intent.category.BROWSABLE" />
     <data
       android:scheme="file"
       android:host="*"
       android:pathPrefix="sdcard/Android/data/android.website/files/pages"
     />
    </intent-filter>

hoping that it would redirect the page requests to the activity, but it doesn't work. The url is still loaded by the browser.

Using shouldOverredeUrlLoading doesn't work well because the link that is loaded is the one of the internal frame. I tried loading instead javascript code that sets that url as the src of the iframe, but didn't work.

It's weird that this only happens on the 2.2 phone. In a 2.2 emulator it works fine.

Any ideas?

4条回答
该账号已被封号
2楼-- · 2019-04-01 01:09

Just confirming that overriding the shouldOverideUrlLoading method (as described above) worked for me as well...I happen to be using phonegap, so i needed to override phonegap's GapViewClient object as shown below. Same basic concept though.

public class XXXX_Activity extends DroidGap {
/** Called when the activity is first created. 
 * @param view */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/RLO/index.htm");
    // Overide selected methods in  webview client used by phone gap to prevent
    // iframe replacements from taking over entire web page.
    super.appView.setWebViewClient(new DroidGap.GapViewClient(this) {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });        
}
查看更多
对你真心纯属浪费
3楼-- · 2019-04-01 01:14

I believe shouldOverrideUrlLoading only gets called for the initial page URL -- not iFrames. Check out onPageFinished (also in the WebViewClient). That generally gets called for each iFrame, although I have seen differences in behavior between Android 2.1 and 2.2.

Note that onPageFinished returns void, so there is no way to tell the WebView not to draw the frame -- and in any event it should have drawn it by that point. But you could always monitor URLs in onPageFinished, then when you see the URL of interest, hide the WebView (where you may need to switch to another view) and then start your new activity.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-01 01:30

Android 4.x seems to behave as follows:

shouldOverrideUrlLoading is called only when the whole page is about to change.

The iframe is only one part of the page. Therefore shouldOverrideUrlLoading is not called.

Overriding public WebResourceResponse shouldInterceptRequest(WebView view, String url) worked for me.

There seems to be no proper way to stop the WebResourceResponse. You can load another URL instead though:

public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

            if( url.contains("myFancyUrl") ) {

                // Do something

                try {
                    return new WebResourceResponse("", "", new URL("http://myserver.com/myrootdocument.html").openStream());
                } catch(Exception exception) {
                    Log.e( exception.toString() );
                }

            }

            return super.shouldInterceptRequest(view, url);

        }
查看更多
forever°为你锁心
5楼-- · 2019-04-01 01:32

Try providing your own WebViewClient to WebView. You should override shouldOverrideUrlLoading(..) and return false.

查看更多
登录 后发表回答