When developing an iOS application using Xamarin.iOS, we have a WKWebView that displays our own website inside.
We want to be able to intercept HTTP requests sent from the WKWebView's JS and extract the request body. Specifically for Form Post requests.
I know we can catch the URL for the requests using the WKNavigationDelegate, but is there a way to capture the whole request?
Thanks,
Andy
Answers
Firstly we need to notify this js post request on native, I find this post talking about this. Here is the js code:
In this callback method, we can pass the data to native. Two ways to achieve that:
At last we need to inject this js to our WKWebView:
after long search what did actually work for me is to edit the form loaded in the wkwebview and make the post with an ajax call.
POST Request Over Webview
For WkWebview
[assembly: ExportRenderer(typeof(PaymentWebview), typeof(PaymentWebViewRenderer))]
namespace MMFInvestorApp.iOS.Utils
{
public class PaymentWebViewRenderer : WkWebViewRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
}
For UIWebview (Deprecated from April 2020
[assembly: ExportRenderer(typeof(PaymentWebview), typeof(PaymentWebViewRenderer))]
namespace MMFInvestorApp.iOS.Utils
{
public class PaymentWebViewRenderer : WebViewRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
}