Question

I'm displaying the modal app store in my app, and everything is working as expected. However, I want to be able to dismiss the modal if it is present when the user closes the app. Is this possible?

This is how I have it set up at the moment:

if (param != nil && NSClassFromString(@"SKStoreProductViewController"))
{
    NSDictionary *appParameters = @{ SKStoreProductParameterITunesItemIdentifier: param };

    SKStoreProductViewController *productViewController = [[SKStoreProductViewController alloc] init];
    [productViewController setDelegate:self];
    [productViewController loadProductWithParameters:appParameters
                                             completionBlock:^(BOOL result, NSError *error)
    {

    }];
    [self presentViewController:productViewController
                       animated:YES
                       completion:^{

                             }];         
}

And this completes the setup by allowing the user to dismiss the modal by clicking the close button.

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
    [viewController dismissViewControllerAnimated:YES completion:nil];
}

I was thinking I could change it so SKStoreProductViewController *productViewController is a member variable, and just call a function to dismiss it when the app deactivates, however this would not compile for anything under iOS 6, correct?

Was it helpful?

Solution

Just retain a reference to that view controller in your view controller:

@property (nonatomic, strong) UIViewController * skStoreProductViewController;

Then create the product view controller:

// Probably in -viewDidLoad ?
if (param != nil && NSClassFromString(@"SKStoreProductViewController"))
{
    self.skStoreProuctViewController = [[SKStoreProductViewController alloc] init];

    // etc...
}

Now, when the user backgrounds your app there is a notification for that event that you can listen for in your view controller. Set up a selector to run and use it to dismiss your view controller:

// Probably in -viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouldDismiss:) name:UIApplicationDidEnterBackgroundNotification object:nil];

Then ...

- (void)shouldDismiss:(NSNotification*)notification {

    [self.skStoreProductViewController dismissViewControllerAnimated:YES completion:nil]  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top