From b403da0d85c83558eabb326cef3621190059bf66 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Mon, 22 Oct 2018 21:54:54 -0400 Subject: [PATCH] [uikit] Preserve method associated with `updateSearchResultsForSearchController:`. Fixes #5024 (#5027) This method is called back from iOS (or tvOS) so there's no managed reference pointing to it. However we know that if it's (private inner) type is present it's because the callback (from native) is possible so we can preserve the method conditionally (to the type presence). https://github.com/xamarin/xamarin-macios/issues/5024 --- src/UIKit/UISearchController.cs | 3 +++ tests/linker/ios/link sdk/LinkSdkRegressionTest.cs | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/UIKit/UISearchController.cs b/src/UIKit/UISearchController.cs index 0a6b7b28a4ac..ea8a37bcf610 100644 --- a/src/UIKit/UISearchController.cs +++ b/src/UIKit/UISearchController.cs @@ -5,6 +5,7 @@ #if !WATCH using System; +using Foundation; namespace UIKit { @@ -17,6 +18,8 @@ public __Xamarin_UISearchResultsUpdating (Action cback) this.cback = cback; IsDirectBinding = false; } + + [Preserve (Conditional = true)] // called back from native, no direct managed reference (except on the type itself) public override void UpdateSearchResultsForSearchController (UISearchController searchController) { cback (searchController); diff --git a/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs b/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs index 423ea39eb3b4..036aa3c9c052 100644 --- a/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs +++ b/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs @@ -1038,6 +1038,20 @@ public void TlsProvider_Apple () Assert.NotNull (provider, "provider"); Assert.That (provider.ID, Is.EqualTo (new Guid ("981af8af-a3a3-419a-9f01-a518e3a17c1c")), "correct provider"); } + + [Test] + public void Github5024 () + { + TestRuntime.AssertXcodeVersion (6,0); + var sc = new UISearchController ((UIViewController) null); + sc.SetSearchResultsUpdater ((vc) => { }); + + var a = typeof (UISearchController).AssemblyQualifiedName; + var n = a.Replace ("UIKit.UISearchController", "UIKit.UISearchController+__Xamarin_UISearchResultsUpdating"); + var t = Type.GetType (n); + Assert.NotNull (t, "private inner type"); + Assert.IsNotNull (t.GetMethod ("UpdateSearchResultsForSearchController"), "preserved"); + } #endif // !__WATCHOS__ [Test]