Bug 59247 - WKNSURLAuthenticationChallenge could not be registered
Summary: WKNSURLAuthenticationChallenge could not be registered
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 10.99 (xcode9)
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 15.5
Assignee: Sebastien Pouliot
: 59279 ()
Depends on:
Reported: 2017-09-06 09:14 UTC by simon.offord
Modified: 2017-09-21 02:51 UTC (History)
9 users (show)

Is this bug a regression?: No
Last known good build:

Stacktrace of crash (2.07 KB, text/plain)
2017-09-06 09:14 UTC, simon.offord
Diagnostic Build Output (1.41 MB, text/plain)
2017-09-06 11:46 UTC, simon.offord
Version Info (2.16 KB, text/plain)
2017-09-06 11:50 UTC, simon.offord

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:

Description simon.offord 2017-09-06 09:14:58 UTC
Created attachment 24588 [details]
Stacktrace of crash

Using the latest xcode 9 preview version (
When trying to setup an Authentication Challenge callback in a WKNavigationDelegate I get the following error message:

 "ObjCRuntime.RuntimeException has been thrown

  The ObjectiveC class 'WKNSURLAuthenticationChallenge' could not be registered, it does not seem to derive from  any known 
  ObjectiveC class (including NSOjbect)."

(find the stacktrace attached)

To reproduce:

Create a custom WKNavigationDelegate
Override the following function:
  public override void DidReceiveAuthenticationChallenge (WKWebView webView, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)

Create a WKWebView.
Assign the WKNavigationDelegate to it.
Load a url request.
Comment 1 Manuel de la Peña [MSFT] 2017-09-06 09:58:05 UTC

Please include your full build logs, test case (to reproduce) and all version information.

To get full build logs just set the log verbosity to diagnostic at the following locations:
- On Visual Studio for Mac: Preferences > Projects > Build
- On Visual Studio for Windows: Tools > Options > Projects and Solutions > Build and Run

On Visual Studio Windows you also want to add `-v -v -v -v` to the mtouch additional arguments by right-clicking the project in the solution explorer and selecting `Properties`.
Note: this is done automatically on Visual Studio for Mac when the log verbosity is set to diagnostic.

Easiest way to get exact version information:
- On Visual Studio for Mac: "Visual Studio" menu, "About Visual Studio" item, "Show Details" button.
- On Visual Studio for Windows: "Help menu", "About Microsoft Visual Studio" item.
Then copy/paste the version information (you can use the "Copy Information" button).
Comment 2 simon.offord 2017-09-06 11:46:55 UTC
Created attachment 24591 [details]
Diagnostic Build Output
Comment 3 simon.offord 2017-09-06 11:50:15 UTC
Created attachment 24593 [details]
Version Info
Comment 4 simon.offord 2017-09-06 11:51:20 UTC
Version and Build Output attached.

There's a simple testcase here:
Comment 5 alan.grgic 2017-09-11 17:58:47 UTC
FYI This looks like a duplicate of #59279
Comment 6 Alex Soto [MSFT] 2017-09-12 05:26:24 UTC
*** Bug 59279 has been marked as a duplicate of this bug. ***
Comment 7 Alex Soto [MSFT] 2017-09-12 05:36:33 UTC
It seems that `WKNSURLAuthenticationChallenge` is a subclass of `WKObject` and `WkObject` is now (as iOS 11) a subclass of `NSProxy`[0] which is not supported by the Xamarin Runtime and we do not have plans yet to support it.

This being said here is a workaround. Just place this class inside your project and the runtime should be able to instantiate and walk the object hierarchy just fine.

[Register ("NSProxy", true)]
public class NSProxy : NSObject {

[0]: https://trac.webkit.org/changeset/219764/webkit
Comment 8 Alex Soto [MSFT] 2017-09-12 12:27:13 UTC
While we have a workaround lets aim for a better solution.
Comment 9 alan.grgic 2017-09-12 13:45:50 UTC
@Alex Soto, I'm happy to put the workaround in place but just to confirm -- there's no risk of the API for this being removed from Xamarin.iOS due to NSProxy not being supported, right? Thanks.
Comment 10 Alex Soto [MSFT] 2017-09-12 13:53:47 UTC
Yeah WK and friends aren't going anywhere. The only "WK" that could go away at some point is the workaround ;)
Comment 11 simon.offord 2017-09-12 15:06:22 UTC
Thanks Alex, that workaround works for us.
Comment 12 alan.grgic 2017-09-13 15:29:40 UTC
@Alex Soto will this workaround only work with the latest preview? I was hoping i could apply this change on the current stable version to make it "forward compatible" but I'm getting "duplicate interface definition for class 'NSProxy'". It's fine if this is the case, I just want to be sure that I'm not just doing something wrong before I go through the trouble of updating to the preview channel.
Comment 13 Joe Taylor 2017-09-13 16:21:41 UTC
I have the latest preview installed and receive the same error as Alan only when targeting a physical device. The simulator works fine. Any thoughts?
Comment 14 alan.grgic 2017-09-13 17:51:00 UTC
I can confirm the same behavior as Joe on latest stable. The build completes successfully when targeting iPhoneSimulator but fails when targeting iPhone (even if its a generic device and not a specific one).
Comment 15 alan.grgic 2017-09-13 21:42:15 UTC
@Joe, I was able to get this to build when targeting a real device when I also added "--registrar:dynamic" to my additional mtouch arguments. Hopefully this helps whoever may still be facing the issue.

See here for more info: https://developer.xamarin.com/guides/ios/advanced_topics/registrar/
Comment 16 Alex Soto [MSFT] 2017-09-14 05:34:59 UTC
Hello guys, you will need a new build ASAP this is merged https://github.com/xamarin/xamarin-macios/pull/2693

I would advice against using --registrar:dynamic for device builds because it is optimized just for the simulator.
Comment 17 Sebastien Pouliot 2017-09-15 20:40:03 UTC
This was merged in https://github.com/xamarin/xamarin-macios/commit/46c6a7d4d7f1acd5a259a87bb6864228787b14de

The package from [1] has the fix but has not yet been fully tested. If you use it please let us know it solve your issue and if you hit any other issue. Thanks!

[1] http://xamarin-storage/macios-mac-xcode9/15/152b654a628e4ea0e6afe8a638a0bada1f3f4be4/xamarin.ios-
Comment 19 alan.grgic 2017-09-16 04:24:57 UTC
I can confirm that installing that package resolved the issue in my case. Thanks for all your work on this!
Comment 20 Michael Rumpler 2017-09-16 12:43:04 UTC
Unfortunately it doesn't work for me.

When I build my sample app from bug 59279 without linking, then it works. But when I enable linking, then it crashes.
I cannot add

    [Preserve (typeof (NSProxy))]

to my implementation of the WKNavigationDelegate because NSProxy is internal.

When the app crashes I get this in the VS Debug window:

2017-09-16 14:23:07.600 WebKit_sample1[9816:84465] warning: Can't find custom attr constructor image: /Users/michael/Library/Developer/CoreSimulator/Devices/777DBBFA-BC18-4E55-B889-35F34151A3EF/data/Containers/Bundle/Application/0B077342-A5F2-4AAF-80BB-591AB6A298B9/WebKit_sample1.app/.monotouch-64/System.dll mtoken: 0x0a000012 due to: Could not resolve type with token 01000013 (from typeref, class/assembly System.Reflection.AssemblyKeyFileAttribute, mscorlib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e) assembly:mscorlib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e type:System.Reflection.AssemblyKeyFileAttribute member:<none>
2017-09-16 14:23:07.723 WebKit_sample1[9816:84603] warning: Can't find custom attr constructor image: /Users/michael/Library/Developer/CoreSimulator/Devices/777DBBFA-BC18-4E55-B889-35F34151A3EF/data/Containers/Bundle/Application/0B077342-A5F2-4AAF-80BB-591AB6A298B9/WebKit_sample1.app/.monotouch-64/System.dll mtoken: 0x0a000017 due to: Could not resolve type with token 0100001b (from typeref, class/assembly System.Diagnostics.ConditionalAttribute, mscorlib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e) assembly:mscorlib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e type:System.Diagnostics.ConditionalAttribute member:<none>
Debuggee returned error code 200.
Mono.Debugger.Soft.CommandException: Debuggee returned error code 200.
   at Mono.Debugger.Soft.VirtualMachine.ErrorHandler(Object sender, ErrorHandlerEventArgs args)
   at Mono.Debugger.Soft.Connection.SendReceive(CommandSet command_set, Int32 command, PacketWriter packet)
   at Mono.Debugger.Soft.Connection.Method_GetCustomAttributes(Int64 id, Int64 attr_type_id, Boolean inherit)
   at Mono.Debugger.Soft.MethodMirror.GetCAttrs(TypeMirror type, Boolean inherit)
   at Mono.Debugger.Soft.MethodMirror.GetCustomAttributes(TypeMirror attributeType, Boolean inherit)
   at Mono.Debugging.Soft.SoftDebuggerBacktrace.CreateStackFrame(StackFrame frame, Int32 frameIndex)
   at Mono.Debugging.Soft.SoftDebuggerBacktrace.GetStackFrames(Int32 firstIndex, Int32 lastIndex)
   at Mono.Debugging.Client.Backtrace.GetFrame(Int32 index, Int32 fetchMultipleCount)
   at Mono.Debugging.Client.Backtrace..ctor(IBacktrace serverBacktrace)
   at Mono.Debugging.Soft.SoftDebuggerSession.GetThreadBacktrace(ThreadMirror thread)
   at Mono.Debugging.Soft.SoftDebuggerSession.HandleBreakEventSet(Event[] es, Boolean dequeuing)
   at Mono.Debugging.Soft.SoftDebuggerSession.HandleEventSet(EventSet es)
   at Mono.Debugging.Soft.SoftDebuggerSession.EventHandler()
The app has been terminated.

Although this only looks like the debugger crashed, it also crashes without debugging. I just don't get any message.
Comment 21 alan.grgic 2017-09-16 13:03:42 UTC
@Michael Rumpler, I think you may have misunderstood. The fix, once installed, removes the need for any code workaround -- you shouldn't need to manually define NSProxy or add a Preserve.
Comment 22 alan.grgic 2017-09-16 13:07:09 UTC
Just be sure that you have the fix and the xcode9 GM installed (and VS for mac pointed to the xcode9 install if you chose to have multiple xcodes installed side by side)
Comment 23 Michael Rumpler 2017-09-16 13:11:19 UTC
I did understand that. Nevertheless, as it didn't work, I tried the PreserveAttribute as Sebastien described in https://github.com/xamarin/xamarin-macios/pull/2693

Strange thing is, that it does work in my main app. It only crashes in the repro app I built. So I don't deem it very important at the moment.
Comment 24 Alex Soto [MSFT] 2017-09-18 19:50:08 UTC
The sample I used to reproduce is the one attached to this bug report https://bugzilla.xamarin.com/show_bug.cgi?id=59279 which is already marked as duplicate of this bug
Comment 25 alan.grgic 2017-09-18 20:00:59 UTC
I didn't make it clear in my comment, so just for clarity's sake, the fix works for me for both the simulator and real devices.
Comment 26 Sebastien Pouliot 2017-09-20 21:02:08 UTC
The fix for XI 11.4 (15.5) will be slightly different so WebKit does not get linked unless needed.

master https://github.com/xamarin/xamarin-macios/pull/2732