ios 11升级后,WKWebView无法加载我的网站。 我在渲染器中添加了以下方法:
[Export("webViewWebContentProcessDidTerminate:")]
public virtual void ContentProcessDidTerminate(WKWebView webView)
{
Console.WriteLine("ContentProcessDidTerminate");
}
[Export("webView:didFinishNavigation:")]
public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
Console.WriteLine("DidFinishNavigation");
}
[Export("webView:didFailNavigation:withError:")]
public void DidFailNavigation(WKWebView webView, WKNavigation navigation, NSError error)
{
// If navigation fails, this gets called
Console.WriteLine("DidFailNavigation:" + error.ToString());
}
[Export("webView:didFailProvisionalNavigation:withError:")]
public void DidFailProvisionalNavigation(WKWebView webView, WKNavigation navigation, NSError error)
{
// If navigation fails, this gets called
Console.WriteLine("DidFailProvisionalNavigation" + error.ToString());
}
[Export("webView:didStartProvisionalNavigation:")]
public void DidStartProvisionalNavigation(WKWebView webView, WKNavigation navigation)
{
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
// When navigation starts, this gets called
Console.WriteLine("DidStartProvisionalNavigation");
}
该应用程序显示了didStartProvisionalNavigation,但它没有移动到其他方法。
我还在info.plist中添加了以下内容:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我还尝试在我的其他网站上运行我的WKWebview,并且一个工作正常。
在Safari中,将它附加到我的WKWebView后,我可以看到一些webapi调用似乎没有完成请求。但在某些情况下,相同的请求确实完成了,但其他一些请求却没有完成。
是否有人遇到此类问题?
任何帮助解决这个问题都会非常感激。
谢谢,
赫克托耳更新:我找到了解决方案
事实证明,在iOS 11中,WKWebView正试图导航到“about:blank”。我不知道我们的网站有那个“页面”。 然后在我的DecidePolicy中,该页面不被允许,我想这也阻止了我的网站继续正常加载。我添加了一个条件来启用导航到“about:blank”并解决了它。
[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
{
var navType = navigationAction.NavigationType;
var targetFrame = navigationAction.TargetFrame;
var url = navigationAction.Request.Url;
if (
url.ToString().StartsWith("http") && (targetFrame != null && targetFrame.MainFrame == true)
)
{
decisionHandler(WKNavigationActionPolicy.Allow);
}
else if (
(url.ToString().StartsWith("http") && targetFrame == null)
||
url.ToString().StartsWith("mailto:")
|| url.ToString().StartsWith("tel:")) //Whatever your test happens to be
{
//decisionHandler(WKNavigationActionPolicy.Allow);
Device.BeginInvokeOnMainThread(() =>
{
UIApplication.SharedApplication.OpenUrl(url);
}
);
} // NEED THIS IN IOS 11
else if ( url.ToString().StartsWith("about") )
{
decisionHandler(WKNavigationActionPolicy.Allow);
}
}
希望这有助于其他人。 :)