I'm displaying an OAtuh2 HTML page in WebView that returns me, after clicking on a validation button that is on this page, a redirect URL that I would like to intercept to use the information from it.
in XML file
<ContentPage.Content>
<WebView x:Name="browser"></WebView>
</ContentPage.Content>
in CS file
browser.Source = "https://myUrl";
My low knowledge in Xamarin doesn't allow me to know how to do it
Thanks for your help
CodePudding user response:
You can do as ToolmakerSteve mentioned using WebNavigatingEventArgs
.
And for details you can implement this on each specific platform with custom renderer .
iOS
[assembly: ExportRenderer(typeof(WebView), typeof(MyRenderer))]
namespace FormsApp.iOS
{
class MyRenderer : WkWebViewRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
this.NavigationDelegate = new MyDelegate();
}
}
public class MyDelegate : WKNavigationDelegate
{
public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
{
if(navigationAction.NavigationType == WKNavigationType.Other)
{
if(navigationAction.Request.Url != null)
{
//do something
}
decisionHandler(WKNavigationActionPolicy.Cancel);
return;
}
decisionHandler(WKNavigationActionPolicy.Allow);
}
}
}
Android
[assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(MyRenderer))]
namespace FormsApp.Droid
{
class MyRenderer : WebViewRenderer
{
public MyRenderer(Context context):base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.SetWebViewClient(new MyClient());
}
}
}
public class MyClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
//do something
return true;
}
}
}
Refer to