[IAP] StoreController Connection & Lifecycle Management Revealed
Solution
Unity 2021.3.x - Unity 6.3.x
Published Fri, Mar 13
Developers using Unity IAP 5.0 often encounter uncertainty regarding the StoreController.Connect method, specifically around the necessity of exception handling, the behavior of connection tasks, and whether OnStoreDisconnected facilitates automatic reconnection.
In Unity IAP 5.0, StoreController.Connect handles errors internally, requiring developers to implement manual reconnection logic within the OnStoreDisconnected callback.
Avoid wrapping StoreController.Connect in try/catch blocks. The Unity IAP library manages internal exceptions and communicates failures through the IStoreListener.OnInitializeFailed callback instead of throwing standard errors.
- Initiate the store link by calling StoreController.Connect.
- Handle
IStoreListener.OnInitializeFailedto capture errors occurring during the initial handshake. - Implement
OnStoreDisconnectedto detect when a previously established connection is lost.
Manual reconnection logic must be implemented by the developer. When OnStoreDisconnected is triggered, your script should evaluate the network state and re-invoke StoreController.Connect if the session needs to be restored.
Additional Tips
- Verify that UnityServices are fully initialized before attempting to call StoreController.Connect.
- Differentiate between
InitializationFailureReasonvalues to determine if an error is transient or requires user account changes. - Use a retry counter or exponential back-off when re-invoking StoreController.Connect to prevent excessive API calls during prolonged outages.
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.