dimanche 4 décembre 2016

Webview Edge vs IE, ScriptNotify working on WP 8.1 but not Windows 10, when the Edge Webview throws you a little curve ball and how to fix it [HTML,C#]

I need to use a Webview in my app so that the application could communicate with my Webview and vise versa.  In my previous article I explained how you can call JavaScript methods from an app [Here], but what happens when the Webview wants to send you some kind of information.

You will tell me, well just use:


 and you are good to go! Or use like you did in your previous article:


However I had the constraint that I am not the one developing the Html and JavaScript that is being called in the Webview, and thus had to work with what I was given.

What you must now is that for Windows Phone 8.1 the Webview uses IE and for Windows 10 the Webview uses Edge.

So, lets head back to my story, on Window Phone 8.1 and Windows 8.1 when I executed a html page in my Webview that had:

function SendTestNotify() {
   if(typeof window.external.notify !== 'undefined')
     window.external.notify('test info');

My Webview would send me back events through ScriptNotify.  However on Windows 10 this code will not send you anything...

So going back to the basic I test this on Windows Phone 8.1

 window.external.notify(typeof window.external.notify);

And got a 'undefined' oddly when you test this on Windows 10 you also get  'undefined' ... So now you can start thinking that I messed up somewhere, however I have found out that trying this code on Windows Phone and on Windows 10 will also not give you the same result:

 window.external.notify(typeof window.external.notify !== 'undefined');

On Windows Phone 8.1 this will send you true and on Windows 10 false.  So I have concluded that the Windows 10 Webview is weird. It can tell me the type of window.external.notify however the JavaScript doesn't seem to be able to detect what typeof it is.

All in all, there will need to be specific code for Windows 10 because its Webview does not behave like the other Webview  on windows 8.x.  Thus this part of the JavaScript code does not work as expected on Windows 10:

if(typeof window.external.notify !== 'undefined') 

Happy Coding,