Search code examples
javascriptxamarinxamarin.formsxlabsxamarin.forms.labs

XLabs.Forms.Controls.HybridWebView does not show html/js Alert


I'm implementing the hybridWebView into our xamarin forms app. The problem i've come across is that the hybridWebView doesn't show the alert messages from the website that is in the webView. I've just got a simple html page which has the following js function:

function testAlert() {
    alert('tester');
}

When I call this using CallJsFunction nothing occurs. I know it's firing because if i put something else in the function to say update the html on the page then it fires just fine.

Is there any properties i need to set to show alerts in the view. I know the built in Xamarin forms webview shows alerts just fine.


Solution

  • I have created a sample on GitHub which basically does what you want in two ways when using HybridWebView for XLabs:

    1. Executing the CallJsFunction() method (bound to the Clicked event of a Button seen here)
    2. Utilising the InjectJavaScript() method (bound to the Clicked event of a Button seen here)
    3. Overriding OnAppearing for the ContentPage, then calling LoadFromContent() with a simple .html page are both stored locally for iOS and Android

    As a note, I had to create a dependency called FilePath to be able to retrieve the path to the .html file for both Android and iOS as the file systems are different.

    This approach worked fine for Android, but not for iOS. I, much like you, didn't see any alert. For that reason, I have created an issue on the Github Issue Tracker for XLabs. I know it's not much right now, but hopefully they'll get back with a solution. If not, it should be possible to simply change their Custom Renderer for the HybridWebView for iOS and thereby resolve the issue.