如何截获来自WKWebview的XHR呼叫?
以下内容仅适用于Webview URL,不适用于WKWebView中托管的站点进行的XHR调用。
[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
{
if (Reference == null || !Reference.TryGetTarget(out FormsWebViewRenderer renderer)) return;
if (renderer.Element == null) return;
var response = renderer.Element.HandleNavigationStartRequest(navigationAction.Request.Url.ToString());
if (response.Cancel || response.OffloadOntoDevice)
{
if (response.OffloadOntoDevice)
AttemptOpenCustomUrlScheme(navigationAction.Request.Url);
decisionHandler(WKNavigationActionPolicy.Cancel);
}
else
{
decisionHandler(WKNavigationActionPolicy.Allow);
renderer.Element.Navigating = true;
}
}
我发现WKURLSchemeHandler可能会有所帮助,但我不知道如何使用它。
有人可以帮忙吗?