Bug 26432 - Events for SKProductsRequest not being Raised in the Unified API Version
Summary: Events for SKProductsRequest not being Raised in the Unified API Version
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.6.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 8.10
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks:
 
Reported: 2015-01-26 16:04 UTC by Kevin Mullins
Modified: 2015-02-19 19:00 UTC (History)
4 users (show)

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

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 FIXED

Description Kevin Mullins 2015-01-26 16:04:09 UTC
When switching to the Unified API the ReceivedResponse and RequestFailed events are never raised on the SKProductsRequest class (they are raised correctly in the Classic API). It looks like the WeakDelegate is being garbage collected before the events can be raised. See this forum post: http://forums.xamarin.com/discussion/31507/xamarin-inapppurchase-receivedresponse-not-invoked-storekit-bindings-problem#latest

The issue can be solved with the following hack:

using System;

#if __UNIFIED__
using Foundation;

namespace StoreKit
#else
using MonoTouch.Foundation;

namespace MonoTouch.StoreKit
#endif
{
	/// <summary>
	/// This class is used as a temporary hack to fix an issue with the weak delegate on the <c>SKProductsRequest</c> object
	/// being garbage collected before events can be fired.
	/// </summary>
	public class InAppProductsRequest : SKProductsRequest
	{
		#region Private Variables
		private NSObject _weakDelegate;
		#endregion

		#region Computed Properties
		// HACK: This class overrides the WeakDelegate to keep it from being Garbage Collected before events agains the <c>SKProductsRequest</c> can be fired.
		/// <summary>
		/// Gets or sets the weak delegate.
		/// </summary>
		/// <value>The weak delegate.</value>
		public override NSObject WeakDelegate {
			get {
				return _weakDelegate;
			}
			set {
				_weakDelegate = value;
			}
		}
		#endregion

		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="MonoTouch.StoreKit.InAppProductsRequest"/> class.
		/// </summary>
		/// <param name="pis">Pis.</param>
		public InAppProductsRequest (NSSet pis) : base(pis)
		{
		}
		#endregion

	}
}

And calling InAppProductsRequest instead of the existing SKProductsRequest, then the ReceivedResponse and RequestFailed events will be fired. 

The source code for our Xamarin.InAppPurchase component (available in the Xamarin Component Store) shows the issue and the temp hack fix: https://github.com/xamarin/private-samples/tree/master/Xamarin.InAppPurchase

Look under the Xamarin.InAppPurchase-Unified project in the Solution in the InAppPurchaseManager.cs file. Search for InAppProductsRequest.

NOTE: Please don't modify that github project directly as it is the live component in the store :)
Comment 2 Sebastien Pouliot 2015-02-19 19:00:36 UTC
so it worked on classic because of the `new` in:

> public new event EventHandler<SKRequestErrorEventArgs> RequestFailed {

where normal inheritance is used in unified (i.e. the event is not redefined). That little change made the original issue more apparent but classic support still had some issue (and the fix correct both cases)

That was fixed (by Chris) in maccore/master 9aab4bb4c49548f3b6bb6edf79b3554e4cc6db4d

QA: unit tests added in 724914c72dd459df618d7b14ab1247ce9d5ab971