From 650c4945a307e3324b7b70acf2ab1973fc31fd61 Mon Sep 17 00:00:00 2001 From: "Emilien Dup." Date: Mon, 15 Dec 2025 13:56:01 +0100 Subject: [PATCH] fix/iOS: Load realm-wrapper DLL using MainBundle.PrivateFrameworksPath Using Reflection to evaluate MainBundle.PrivateFrameworksPath on iOS --- Realm/Realm/Native/NativeCommon.cs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Realm/Realm/Native/NativeCommon.cs b/Realm/Realm/Native/NativeCommon.cs index f389d9e1b2..ff532aea84 100644 --- a/Realm/Realm/Native/NativeCommon.cs +++ b/Realm/Realm/Native/NativeCommon.cs @@ -23,7 +23,9 @@ using System.Threading; using Realms.Helpers; using Realms.Logging; -#if !NET5_0_OR_GREATER +#if NET5_0_OR_GREATER +using System.Reflection; +#else using System.IO; #endif @@ -50,7 +52,26 @@ internal static void Initialize() { if (libraryName == InteropConfig.DLL_NAME) { - libraryName = "@rpath/realm-wrappers.framework/realm-wrappers"; + var rpath = "@rpath"; + + var runtimeVersion = Environment.Version; + if (runtimeVersion.Major >= 10) + { + // .NET 10 breaking change: Realm can't use @rpath for runtime library location. + // Solution: Use NSBundle.MainBundle.PrivateFrameworksPath + // Currently it is evaluated through reflection, but could be replaced when platform code evaluation/injection improves in future versions. + var nsBundleType = Assembly.Load("Microsoft.iOS").GetType("Foundation.NSBundle"); + var mainBundleProperty = nsBundleType?.GetProperty("MainBundle", BindingFlags.Public | BindingFlags.Static); + var mainBundle = mainBundleProperty?.GetValue(null); + var privateFrameworksPathProperty = nsBundleType?.GetProperty("PrivateFrameworksPath", BindingFlags.Public | BindingFlags.Instance); + + if (privateFrameworksPathProperty?.GetValue(mainBundle) is string privateFrameworksPath) + { + rpath = privateFrameworksPath; + } + } + + libraryName = $"{rpath}/realm-wrappers.framework/realm-wrappers"; } return NativeLibrary.Load(libraryName, assembly, searchPath);