Search code examples
iosdismissviewcontroller

iOS: Child View Controller dismissal causes viewDidLoad to be called again


I have a view controller, SubscribeVC which uses InAppPurchases service which is injected into it. The user initiates the purchase logic and during the process, StoreKit shows few UIAlertControllers regarding the purchase that is about to be made. On the view controller I hold strong reference to the (InAppPurchases) service, and calls a function which has a completion handler for when the purchase completes.

The problem is that when the user dismisses the StoreKit's alerts, it makes SubscribeVC to be reloaded (Which calls viewDidLoad etc.), and to the completion handler to cause EXC_BAD_ACCESS error (because it was released when the VC instance was) when completes.

I open SubscribeVC through AppDelegate and I also tried to hold a strong reference to the SubscribeVC instance from within the InAppPurchases service but nothing helps, SubscribeVC is still being reloaded some how.

What am I doing wrong? Any Ideas? Thanks!


Solution

  • Well apparently I instantiate the UIViewController SubscribeVC from applicationDidBecomeActive of AppDelegate. When StoreKit's alert was dismissed, applicationDidBecomeActive was called again, reinstantiated SubscribeVC and reassigned AppDelegate's window.rootViewController to the newly instantiated VC, which caused the original VC to be released from memory.

    The solution is to instantiate the view controller from didFinishLaunchingWithOptions, which is being called once when the application launches.

    Hope it'll help someone.