iOS - Reward Throws an Exception "Specified cast is not valid"
Upgraded from 1.7.1 to 2.0.0.5
- Appears to be only on iOS (Android works perfectly)
- Testing an physical device iOS iPad
- CrossMauiMTAdmob.Current.LoadRewarded(xxxx) is called
- Upon the event handler "CrossMauiMTAdmob.Current.OnRewardedLoaded" (code) below the ad is displayed an exception is thrown almost immediately causing a total app crash.
private void OnRewardedLoaded(object? sender, EventArgs e)
{
// Show Reward
if (CrossMauiMTAdmob.Current.IsRewardedLoaded())
CrossMauiMTAdmob.Current.ShowRewarded(); // Exception is thrown quickly after Ad is displayed
}
Work Around
- Downgrade back to 1.7.1 and everything works as it did
Stack Trace
**System.InvalidCastException:** 'Loading...'
Thread finished: <Thread Pool> #29
The thread 29 has exited with code 0 (0x0).
The thread 30 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #30
Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
at ObjCRuntime.RegistrarHelper.ConstructNSObject[FullScreenPresentingAd](Type type, NativeHandle nativeHandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/RegistrarHelper.cs:line 235
at ObjCRuntime.Runtime.ConstructNSObject[FullScreenPresentingAd](IntPtr ptr, Type type, MissingCtorResolution missingCtorResolution, IntPtr sel, RuntimeMethodHandle method_handle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 1527
at ObjCRuntime.Runtime.GetNSObject[FullScreenPresentingAd](IntPtr ptr, IntPtr sel, RuntimeMethodHandle method_handle, Boolean evenInFinalizerQueue) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 1921
at Plugin.MauiMTAdmob.Platforms.iOS.RewardService.__Registrar_Callbacks__.callback_761_Plugin_MauiMTAdmob_Platforms_iOS_RewardService_DidRecordImpression(IntPtr pobj, IntPtr sel, IntPtINFO: r p0, IntPtr* exception_gchandle)
2025-02-26 12:38:37.212 ABC.Mobile[1361:570158] Received unhandled Objective-C exception that was marshalled from a managed exception: Specified cast is not valid. (System.InvalidCastException)
at ObjCRuntime.RegistrarHelper.ConstructNSObject[FullScreenPresentingAd](Type type, NativeHandle nativeHandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/RegistrarHelper.cs:line 235
at ObjCRuntime.Runtime.ConstructNSObject[FullScreenPresentingAd](IntPtr ptr, Type type, MissingCtorResolution missingCtorResolution, IntPtr sel, RuntimeMethodHandle method_handle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 1527
at ObjCRuntime.Runtime.GetNSObject[FullScreenPresentingAd](IntPtr ptr, IntPtr sel, RuntimeMethodHandle method_handle, Boolean evenInFinalizerQueue) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 1921
at Plugin.MauiMTAdmob.Platforms.iOS.RewardService.__Registrar_Callbacks__.callback_7INFO: Closing debug connection from device (USB)INFO: Disposing input and output streams...ERROR: An error occurred while writing to the debug stream. Details: ObjectDisposed_Generic
ObjectDisposed_ObjectName_Name, UsbStreamINFO: *** Terminating app due to uncaught exception 'System.InvalidCastException', reason: 'Specified cast is not valid. (System.InvalidCastException)
at ObjCRuntime.RegistrarHelper.ConstructNSObject[FullScreenPresentingAd](Type type, NativeHandle nativeHandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/RegistrarHelper.cs:line 235
at ObjCRuntime.Runtime.ConstructNSObject[FullScreenPresentingAd](IntPtr ptr, Type type, MissingCtorResolution missingCtorResolution, IntPtr sel, RuntimeMethodHandle method_handle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 1527
at ObjCRuntime.Runtime.GetNSObject[FullScreenPresentingAd](IntPtr ptr, IntPtr sel, RuntimeMethodHandle method_handle, Boolean evenInFinalizerQueue) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 1921
at Plugin.MauiMTAdmob.Platforms.iOS.RewardService.__Registrar_Callbacks__.callback_761_Plugin_MauiMTAdmob_Platforms_iOS_RewardService_DidRecordImpression(IntPtr pobj,INFO: IntPtr sel, IntPtr p0, IntPtr* exception_gchandle)
'
*** First throw call stack:
(0x19bd787cc 0x19904b2e4 0x1069ffcf4 0x1069ff820 0x106c83e34 0x104d84938 0x104cfe7e8 0x19a955cdc 0x19a955b9c 0x19a955998 0x19a953be8 0x19a9cb810 0x19a9cb440 0x1a3a5d2e8 0x1a3a4e0d0 0x1a3a5c9e0 0x1a3a5c5fc 0x19bd4c204 0x19bd49440 0x19bd48830 0x1e7d281c4 0x19e8aeeb0 0x19e95d5b4 0x1069e85f4 0x106c23cd4 0x106c222a8 0x106c17304 0x106c14ed0 0x106be37ac 0x106b8b58c 0x106b91490 0x106bea1c4 0x106a133b0 0x106c549c8 0x1c1736ec8)
INFO: Disposing input and output streams...INFO: 61_Plugin_MauiMTAdmob_Platforms_iOS_RewardService_DidRecordImpression(IntPtr pobj, IntPtr sel, IntPtr p0, IntPtr* exception_gchandle)
INFO: Closing HotReload local tunnel connection from deviceERROR: An error occurred when writing to the output stream. Details: net_io_readfailure, Operation canceledINFO: Disposing console and debugger streams...INFO:
Same here on v2.0.0.5
Thank you. I'll have a look, check and if needed, fix it asap.
I had the same problem but on both 1.7.1 and 2.0.0.5, I think it is a different, more general issue.
BTW, how do you initialize the plugin for iOS? This method is not implemented, and I get the not implemented exxeption when trying to show an ad without initializing the plugin (which as i can see by the plugin's methods definitions is obvious, since CrossMauiMTAdmob.Current without initializing is null):
CrossMauiMTAdmob.Current.Init
Is there another method then for initializing iOS ads?
Hi, Any updates regarding this?
Waiting for a solution.
I'm looking into it.
I'll update you and the plugin asap.
Just a quick update. I've managed to reproduce the issue. It happens only on a real device and not on a simulator. I've tried with many simulators, both iPhones and iPads and it works. As soon as I use a physical device, it crashes. Unfortunately the issue is not on the plugin itself but in the iOS binding to the native SDK so it might take a bit more time but I'm on it. I'll update you asap
Ok, I've just released version 2.0.1 of the plugin. It should solve the issue for iOS.
Let me know if it works for you.
The issue was still present especially on iOS 26. I've updated the plugin to version 2.2 and this should solve the issue.
If the issue should still be present, feel free to open the issue again