请原谅我的新手问题,我是一个非常新的Xamarin开发人员,我不知道Swift,所以Swift中的答案一直没用,因为我还没能将它翻译成C#。我发现Swift中的一些资源与此问题类似,但我不知道Swift,因此无法将代码转换为xamarin解决方案。我无法在我的应用程序使用的WKWebView中查看的网页中找到任何链接。现在,我可以打开具有“target = _blank”属性的链接,但我无法打开任何其他链接,特别是在当前窗口中打开的链接,以及具有“mailto:”和“tel”属性的链接在URL中。我通过将以下类添加到WebViewController以及ViewDidLoad方法的一行来获得target = _blank链接:
public class NavDelegate : WKUIDelegate
{
public override WKWebView CreateWebView(WKWebView webView, WKWebViewConfiguration configuration, WKNavigationAction navigationAction, WKWindowFeatures windowFeatures)
{
var url = navigationAction.Request.Url;
if (navigationAction.TargetFrame == null)
{
webView.LoadRequest(navigationAction.Request);
}
return null;
}
}
以下是添加到ViewDidLoad()方法的行: WebAppView.UIDelegate = new NavDelegate();
完整的ViewDidLoad方法:
public override void ViewDidLoad()
{
base.ViewDidLoad();
WebViewLogo.Owner = this;
//Check System Version and set WebView accordingly
//WKWebView only supported on iOS Versions 11+
if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
{
View.Frame = new CoreGraphics.CGRect(0, 108, 1024, 611);
View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Height, NSLayoutRelation.Equal, View, NSLayoutAttribute.Height, 1.0f, 0.0f));
View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Width, NSLayoutRelation.Equal, View, NSLayoutAttribute.Width, 1.0f, 0.0f));
View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, View, NSLayoutAttribute.Trailing, 1.0f, 0.0f));
View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1.0f, 43.0f));
var WebAppView = new WKWebView(View.Frame, new WKWebViewConfiguration());
WebAppView.UIDelegate = new NavDelegate();
View.AddSubview(WebAppView);
//set the margins and constraints
// Add a tap recognizer to reset timer
var actionGestureRecognizer = new UITapGestureRecognizer(this, new ObjCRuntime.Selector("HandleActionEvent:"));
actionGestureRecognizer.NumberOfTapsRequired = 1;
actionGestureRecognizer.Delegate = new GestureDelegate();
WebAppView.AddGestureRecognizer(actionGestureRecognizer);
// Load the quote
//Attempt to get the current quote if active
var storedQuoteDataAsJson = QuoteManager.FetchQuote();
// If quote is empty or expired pop to the contact screen
if (String.IsNullOrEmpty(storedQuoteDataAsJson) || !QuoteManager.IsQuoteActive((int)NSUserDefaults.StandardUserDefaults.IntForKey("QuoteExpireTimeInSeconds")))
{
AppDelegate.Self.PopToTarget(ControllerScreen.ContactController);
}
// Load the quote
Quote currentQuote = QuoteManager.DeserializeQuote(storedQuoteDataAsJson);
if (currentQuote != null)
{
// NOTE: https required for iOS 9 ATS
var url = String.Format("{0}{1}", NSUserDefaults.StandardUserDefaults.StringForKey("WebURL"), currentQuote.QuoteID);
WebAppView.LoadRequest(new NSUrlRequest(new NSUrl(url)));
}
}
else //UIWebView is not supported on iOS versions less than 11, so must use UIWebView
{
View.Frame = new CoreGraphics.CGRect(0, 108, 1024, 611);
View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Height, NSLayoutRelation.Equal, View, NSLayoutAttribute.Height, 1.0f, 0.0f));
View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Width, NSLayoutRelation.Equal, View, NSLayoutAttribute.Width, 1.0f, 0.0f));
View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, View, NSLayoutAttribute.Trailing, 1.0f, 0.0f));
View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1.0f, 43.0f));
var WebAppView = new UIWebView(View.Frame);
View.AddSubview(WebAppView);
//set the margins and constraints
// Add a tap recognizer to reset timer
var actionGestureRecognizer = new UITapGestureRecognizer(this, new ObjCRuntime.Selector("HandleActionEvent:"));
actionGestureRecognizer.NumberOfTapsRequired = 1;
actionGestureRecognizer.Delegate = new GestureDelegate();
WebAppView.AddGestureRecognizer(actionGestureRecognizer);
// Load the quote
//Attempt to get the current quote if active
var storedQuoteDataAsJson = QuoteManager.FetchQuote();
// If quote is empty or expired pop to the contact screen
if (String.IsNullOrEmpty(storedQuoteDataAsJson) || !QuoteManager.IsQuoteActive((int)NSUserDefaults.StandardUserDefaults.IntForKey("QuoteExpireTimeInSeconds")))
{
AppDelegate.Self.PopToTarget(ControllerScreen.ContactController);
}
// Load the quote
Quote currentQuote = QuoteManager.DeserializeQuote(storedQuoteDataAsJson);
if (currentQuote != null)
{
// NOTE: https required for iOS 9 ATS
var url = String.Format("{0}{1}", NSUserDefaults.StandardUserDefaults.StringForKey("WebURL"), currentQuote.QuoteID);
WebAppView.LoadRequest(new NSUrlRequest(new NSUrl(url)));
}
}
}
非常感谢任何帮助。提前致谢。
答案 0 :(得分:1)
这应该可以使用WKNavigationDelegate
class AllowAllDelegate : WKNavigationDelegate
{
public override void DecidePolicy(WKWebView webview, WKNavigationAction action, Action<WKNavigationActionPolicy> actionCallback)
{
// do some checks if you want to filter actions
actionCallback(WKNavigationActionPolicy.Allow);
}
}
使用
WebAppView.NavigationDelegate = new AllowAllDelegate();
UIWebView与UIWebViewDelegate
相同class AllowAllOldDelegate : UIWebViewDelegate
{
public override bool ShouldStartLoad (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
{
// do some checks if you want to filter actions
return true;
}
}
使用
WebAppView.Delegate = new AllowAllOldDelegate();
如果您使用Weak Delegate mechanism。
,也可以避免创建新课程答案 1 :(得分:0)
我得到以下工作。请记住,我的整个项目都基于Xamarin's "HybridWebView" sample,其中包含适用于iOS和Android的自定义HybridWebViewRenderer类。我还使用了this question原始帖子中的信息。我没有遇到该用户遇到的同样问题。
新课程:
public class MyWKUIDelegate : WKUIDelegate
{
public override WKWebView CreateWebView(WKWebView webView, WKWebViewConfiguration configuration, WKNavigationAction navigationAction, WKWindowFeatures windowFeatures)
{
var url = navigationAction.Request.Url;
if (navigationAction.TargetFrame == null)
{
webView.LoadRequest(navigationAction.Request);
}
return null;
}
}
将我的新MyWKUIDelegate分配给我的WKWebView实例的UIDelegate属性:
var config = new WKWebViewConfiguration { UserContentController = userController };
userController.AddScriptMessageHandler(this, "native");
WKWebView webView = new WKWebView(Frame, config);
webView.UIDelegate = new MyWKUIDelegate();