Search code examples
xamarin.formswebviewxamarin.androidandroid-webview

Links inside of a Xamarin.Forms/Xamarin.Android WebView won't work


I have a Xamarin.Forms app that opens locally saved HTML files which contain relative link to each other (think an old HTML offline docs) in a WebView. Everything functioned in Android 10, but now I needed to make a custom renderer in Xamarin.Android due to the new Android 11 changes needing AllowFileAccess (I have all accesses currently on for testing until I get this problem fixed) in the WebSettings. The WebView's OnElementChange sets all the Control's settings, sets the WebClient (which has a ShouldOverrideUrlLoading set to handle particular filetypes like PDF), and then loads the URL. The first page loads fine, but none of the links operate (this testing page has a few links to other HTML pages and some to PDFs). Click on the highlights them as normal, but nothing opens.

No breakpoints in the WebClient's ShouldOverride are hit and neither are any in the Android WebView's OnElementChanged when I click a link. The output window registers the touch, but doesn't do anything more. I feel like this is some kind of communication problem between Xamarin.Forms WebView and Xamarin.Android WebView (possibly even the Xamarin.Android's WebView not being up to date with Android's WebView), but I do not know where to look. Has anyone run into a similar situation and have a fix to get the links functioning again?

Thanks.


Solution

  • After much blood, sweat, and tears, mostly tears, I found the solution.

    First off, WebView doesn't tell you directly, but anything that targets a new window (IE _blank and potentially others) will not hit your WebViewClient's ShouldOverrideUrlLoading method and instead hit your WebViewChromeClient's OnCreateWindow method. So you need to handle loading through that one as well as through your WebViewClient.

    Second, links will not always work because of Safe Browsing in the WebView. Again, Google doesn't specifically say it can cause issues with urls, but it works against outside urls (which in a file is potentially every url). I had to turn it off, which you can find instructions at developer.android.com/guide/webapps/managing-webview, and I was able to navigate just fine. I feel you will want to set up safeguards to prevent people from abusing your app (like having a WebView that is for your stuff with Safe Browsing off and another for all others, or blocking any sites that aren't yours) since cybersecurity is always a thing.

    Lastly, I found IFrames won't load properly if you manually load the page in the view passed to WebViewClient's ShouldOverrideUrlLoading. The sizing goes all off in my docs, which leads me to believe the elves in the background do something special if you return false and the WebView handles it. I found I needed to handle all my other cases (IE pdfs, images, etc) and then return false from ShouldOverrideUrlLoading if it is my iframe htmls.

    As a note, because I had forgotten this having worked on Windows for so long, remember that Android's base file system is case sensitive, which makes links case sensitive. You need to either makes sure all your casing is correct in your files or have them on a FAT file system SD card if you want them to be insensitive.