Summary:
This article provides guidance on managing Store Connections with the v5 series of In-App Purchasing SDKs. It highlights the differences with previous versions and explains the benefits of the current approach.
Overview:
Version 5 of the In-App Purchasing package represents a significant update from v4. Connection management is explicit, requiring you to manually establish and maintain the link to the underlying store service. This provides developers with direct control over the store connection and is more flexible and robust, allowing you to manage connection failures gracefully.
Cause:
Previously, the v4 IAP system could not recover from store connection failures. A common workaround was for the developer to manually call UnityPurchasing.Initialize() again when an OnInitializeFailed event was received, but this was not a good solution. It was inefficient and prone to loops or errors
Resolution:
In Unity IAP v5, connection management is explicit, requiring you to manually establish and maintain the link to the underlying store service on the device.
You must initiate the connection by calling StoreController.Connect() and subscribe to the OnStoreDisconnected callback event to detect interruptions, such as a Google Play Store service crash or update.
If a disconnection occurs, simply call Connect() again to recover the session; this decoupled approach lets you restore functionality immediately without the overhead of reinitializing the entire SDK or re-fetching product data.
Alternatively, configure the retry policy via the SetStoreReconnectionRetryPolicyOnDisconnection method on the StoreController or StoreService
e.g.
ExponentialBackOffRetryPolicy retryPolicy = new ExponentialBackOffRetryPolicy(500, 60000, 2f);
m_StoreController.SetStoreReconnectionRetryPolicyOnDisconnection(retryPolicy);See SetStoreReconnectionRetryPolicyOnDisonnection(IRetryPolicy?)
Best Practices:
- Always subscribe to OnStoreDisconnected - This is your primary way to know when a disconnection occurs.
-
Implement automatic retry logic - Use the
isRetryablefield on theStoreConnectionFailureDescriptionto determine if automatic reconnection should be attempted. - Track status manually - Since a connection status API isn't available, maintain your own connection state
- Provide user feedback - Show connection status to users, especially during purchases
-
Handle graceful degradation - Disable purchase UI when disconnected, show appropriate messaging