11

I am working with a WebView in a Windows 8.1 xaml app and need to handle navigation to a custom protocol ie. "app://12345".

I have the WebView navigating off to a website for authentication which is then redirecting to this custom protocol as the response.

None of the WebView navigation events are fired and Windows is picking this up and attempting to open an app with it ("Look for an app in the Store" dialog).

Is it possible to catch when the WebView is navigating to this protocol?

dkarzon
  • 7,868
  • 9
  • 48
  • 61
  • 2
    did you solved it somehow please? I'm dealing with the similar issue. – y0j0 Apr 18 '15 at 15:15
  • 1
    Unfortunately this is not possible. At least until Windows 10 Which brings an `MSWebViewUnsupportedUriSchemeIdentified` event - https://msdn.microsoft.com/library/windows/apps/dn803906.aspx?f=255&MSPPError=-2147217396 I haven't tested this yet though. – dkarzon Apr 19 '15 at 22:02
  • thanks for answer. I hope Windows 10 will resolve more issues in windows store development. This case I solved by injecting script see my comment below – y0j0 Apr 20 '15 at 07:21

3 Answers3

0

I had similar issue and I resolved it with this code injected to HTML. Or you can run this code directly on WebView.

for (var i = 0; i < document.links.length; i++) { 
    if(document.links[i].href.indexOf('app') === 0){
        var currentHref = document.links[i].href;
        document.links[i].setAttribute('href', 'javascript:window.external.notify(\'' + currentHref + '\')');   
        document.links[i].removeAttribute('target');
    }       
}

After this you can catch window.external.notify in C# code and do what you want.

private void WebView_OnScriptNotify(object sender, NotifyEventArgs e)
{
     if (e.Value.StartsWith("app"))
     {
         DoAction(e.Value);
         return;
     }
}
y0j0
  • 3,369
  • 5
  • 31
  • 52
  • This is a solution only if you have access to the page source and the link to the protocol is an actual `a` tag and not something from JavaScript – mcont Jun 07 '15 at 13:19
0

Maybe it's an overkill solution, but you can use an IUriToStreamResolver with the method NavigateToLocalStreamUri :https://msdn.microsoft.com/library/windows/apps/dn299344. This solution allow you to create a custom resolver wich implements the IUriToStreamResolver. The resolver will be notified for each ressource required by the WebView. In the resolver, you have to return a Stream for each ressources requested. But you can handle the custom protocol by this way.

t.ouvre
  • 2,856
  • 1
  • 11
  • 17
0

I just found this question when trying to do something similar, and it looks like Windows has implemented UnsupportedUriSchemeIdentified (based on @dkarzon's above comment) so this should work properly now!

S. Matthews
  • 355
  • 2
  • 9