WebView capture navigation to a custom protocol
Asked Answered
L

3

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?

Leanto answered 14/10, 2013 at 3:25 Comment(3)
did you solved it somehow please? I'm dealing with the similar issue.Semantic
Unfortunately this is not possible. At least until Windows 10 Which brings an MSWebViewUnsupportedUriSchemeIdentified event - msdn.microsoft.com/library/windows/apps/… I haven't tested this yet though.Leanto
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 belowSemantic
S
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;
     }
}
Semantic answered 20/4, 2015 at 7:23 Comment(1)
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 JavaScriptNessie
C
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.

Confab answered 23/12, 2015 at 13:48 Comment(0)
J
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!

Jozef answered 18/10, 2016 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.