Bug 35418 - WKWebView DecidePolicyForNavigationAction delegate method
Summary: WKWebView DecidePolicyForNavigationAction delegate method
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.1 (iOS 9.1)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Rolf Bjarne Kvinge [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2015-10-30 15:27 UTC by mwpearce
Modified: 2015-12-03 15:42 UTC (History)
7 users (show)

Tags: BZCU
Is this bug a regression?: ---
Last known good build:


Attachments
Xamarin Studio Logs (5.60 KB, application/zip)
2015-11-30 16:08 UTC, mwpearce
Details
Xamarin Studio Sample Project (15.62 KB, application/zip)
2015-12-01 20:37 UTC, mwpearce
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description mwpearce 2015-10-30 15:27:57 UTC
I'm attempting to create a custom renderer for WKWebView and trying to implement the webView:decidePolicyForNavigationAction:decisionHandler: delegate method.  Everything compiles correctly, but at runtime I get the following error: 

ObjCRuntime.RuntimeException: Unable to locate the block to delegate conversion method for the method XLabs.Forms.Controls.HybridWebViewRenderer.DecidePolicy's parameter #3. Please file a bug at http://bugzilla.xamarin.com.
  at ObjCRuntime.Runtime.GetBlockWrapperCreator (System.Reflection.MethodInfo method, Int32 parameter) [0x001cd] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/ObjCRuntime/Runtime.cs:598
  at ObjCRuntime.Runtime.GetBlockWrapperCreator (IntPtr method, Int32 parameter) [0x00000] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/ObjCRuntime/Runtime.cs:240
  at ObjCRuntime.Runtime.get_block_wrapper_creator (IntPtr method, Int32 parameter) [0x00000] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/runtime/Delegates.generated.cs:106
  at at (wrapper native-to-managed) ObjCRuntime.Runtime:get_block_wrapper_creator (intptr,int)
  at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/UIKit/UIApplication.cs:77
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/UIKit/UIApplication.cs:60
  at MobileDocs.iOS.Application.Main (System.String[] args) [0x00008] in /Users/s791373/Documents/Repos/Mobility/MobileDocs/iOS/Main.cs:17

I have tried to different implementations, one using an Action and one a Delegate:
Action
		[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
		public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
		{
			if (this.Element != null)
			{
				var allow = this.Element.OnDecidePolicy(new Uri(navigationAction.Request.Url.AbsoluteString));
				if (allow)
				{
					decisionHandler(WKNavigationActionPolicy.Allow);
				}
				else
				{
					decisionHandler(WKNavigationActionPolicy.Cancel);
				}
			}
			else
			{
				decisionHandler(WKNavigationActionPolicy.Allow);
			}
		}

Delegate
		public delegate void DecisionHandler(WKNavigationActionPolicy actionPolicy);

		[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
		public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, DecisionHandler decisionHandler)
		{
			if (this.Element != null)
			{
				var allow = this.Element.OnDecidePolicy(new Uri(navigationAction.Request.Url.AbsoluteString));
				if (allow)
				{
					decisionHandler(WKNavigationActionPolicy.Allow);
				}
				else
				{
					decisionHandler(WKNavigationActionPolicy.Cancel);
				}
			}
			else
			{
				decisionHandler(WKNavigationActionPolicy.Allow);
			}
		}
Comment 1 mwpearce 2015-11-30 16:08:23 UTC
Created attachment 14037 [details]
Xamarin Studio Logs
Comment 2 Kent Green [MSFT] 2015-11-30 18:44:07 UTC
Reporting customer from the description sent in the following build info via desk case #230120:

=== Xamarin Studio ===

Version 5.10 (build 871)
Installation UUID: ca8edcbe-3039-4474-a424-3febb385ab47
Runtime:
Mono 4.2.1 (explicit/6dd2d0d)
GTK+ 2.24.23 (Raleigh theme)

Package version: 402010102

=== Xamarin.Profiler ===

Not Installed

=== Apple Developer Tools ===

Xcode 7.1.1 (9081)
Build 7B1005

=== Xamarin.iOS ===

Version: 9.2.1.51 (Business Edition)
Hash: 3c0ec35
Branch: master
Build date: 2015-11-12 13:05:39-0500

=== Xamarin.Mac ===

Not Installed

=== Xamarin.Android ===

Version: 6.0.0.34 (Business Edition)
Android SDK: /Users/s791373/Library/Android/sdk
Supported Android versions:
4.1 (API level 16)
4.2 (API level 17)
4.4 (API level 19)
5.0 (API level 21)
5.1 (API level 22)
6.0 (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)

=== Xamarin Android Player ===

Not Installed

=== Build Information ===

Release ID: 510000871
Git revision: 4e9c5abb5ffdae12ba02ac49da83f8b2011dbb88
Build date: 2015-11-12 06:02:54-05
Xamarin addins: 55007ed0e56436f385d8e26394a45be563abc7e8
Build lane: monodevelop-lion-cycle6

=== Operating System ===

Mac OS X 10.11.1
Darwin Mikes-MacBook-Pro.local 15.0.0 Darwin Kernel Version 15.0.0
Sat Sep 19 15:53:46 PDT 2015
root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
Comment 3 mwpearce 2015-12-01 20:37:11 UTC
Created attachment 14064 [details]
Xamarin Studio Sample Project
Comment 4 mwpearce 2015-12-01 20:37:42 UTC
I have uploaded a sample Xamarin Forms project that produces the error.
Comment 5 Rolf Bjarne Kvinge [MSFT] 2015-12-02 16:36:34 UTC
You need to implement the IWKNavigationDelegate, this gives Xamarin.iOS enough data to lookup the required information for the block callback at runtime:

    	public partial class MyWebViewRenderer : ViewRenderer<MyWebView, WKWebView>, IWKNavigationDelegate
Comment 6 mwpearce 2015-12-03 15:42:29 UTC
That seems to have solved the problem.  Thanks so much for your help!