Modal App Store won't dismiss - ios

I have a button that shows my app in a modal view, so that people can download and rate the app. I got it to show up modally with this code.
NSDictionary *appParameters = [NSDictionary dictionaryWithObject:#"607257427"
forKey:SKStoreProductParameterITunesItemIdentifier];
SKStoreProductViewController *productViewController = [[SKStoreProductViewController alloc] init];
[productViewController setDelegate:self];
[productViewController loadProductWithParameters:appParameters
completionBlock:^(BOOL result, NSError *error)
{
}];
[self presentViewController:productViewController
animated:YES
completion:^{
}];
`
This is what it turns into.
Problem is that the cancel button isn't working, it may be something with the Simulator or also something really simple, but I can't figure out why the cancel button isn't working

You need to implement the delegate method to dismiss the view controller:
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
[viewController dismissViewControllerAnimated:YES completion:nil];
}

Related

View controller not being created

I have a navigation controller that starts my app (rootViewController is the navigationController). Then inside one of the views of the navigation I call:
TabBarController *tab = [[TabBarController alloc] init];
// Presentation
[self presentViewController:tab animated:NO completion:nil];
Then one of the tabs calls the UIImagePickerController and then saves the image on another thread. Then I return to the main queue and run:
dispatch_async(dispatch_get_main_queue(), ^{
[picker dismissViewControllerAnimated:YES completion:nil];
PostViewController *post = [[PostViewController alloc] init];
// Presentation
[self presentViewController:post animated:NO completion:nil];
});
But the post view never gets called and the viewDidLoad never gets hit in the PostViewController.m. Instead the imagePicker disappears and returns to the tabBarController. How do I fix this?
Assuming that you PostViewController object is not nil , Present the view controller after the dismiss process of picker ViewController is completed . Try this Code
dispatch_async(dispatch_get_main_queue(), ^{
[picker dismissViewControllerAnimated:YES completion:^{
PostViewController *post = [[PostViewController alloc] init];
// Presentation
[self presentViewController:post animated:NO completion:nil];
}];
});

Add Subview to SKStoreProductViewController

I am trying to counteract Apple's lack of innovation with SKStoreProductViewController. Im pretty certain this isn't possible, but lets see.
I am presenting the view controller outside of this block:
[storeController loadProductWithParameters:productParameters completionBlock:^(BOOL result, NSError *error) {}];
[self presentViewController:storeController animated:YES completion:^{}];
That way the view controller shows immediately, rather than waiting 30 seconds to load the product. However, now it obviously shows a blank controller while it loads the product. I would love to put a UIActivityIndicatorViewon the window until the product loads. I have tried this:
[self presentViewController:storeController animated:YES completion:^{
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activity startAnimating];
activity.frame = CGRectMake(0, 0, 44, 44);
activity.center = self.view.window.center;
[self.view.window addSubview:activity];
}];
No luck there. Is it possible to add an activity indicator on top of this view?
Have you tried to bring up the view controller once the SKStoreProductViewController loading is completed? like this
[storeController loadProductWithParameters:productParameters completionBlock:^(BOOL result, NSError *error)
{
[self presentViewController:storeController animated:YES completion:^{}];
}];

Dismiss Modal App store on App close

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?
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]
}

remove ViewController from parent screen stays black

I'm having trouble with removing a modal view.
I want to show (after pressing a button) a my own SendMailViewController which it self shows a MFMailComposeViewController. Then and after pressing cancel of send, in my own SendMailView controller in didFinishWithResult i do a [self dismissModalViewControllerAnimated:YES] and that works. The MFMailComposeView goes away.
But then the screen stays black....it think i also have to remove my SendMailViewController from it's parent. That's where i pushed the button...even after [self removeFromParentViewController] it still stays black...
Where do i go wrong?
And yes i would like the extra viewcontroller (SendMailViewController) because that controller will become the delegate of MFMailComposeViewController. Otherwise my caller (controller with the button) get's to much responsibility. Or do i also go wrong here?
Thanks,
/jr00n
- (IBAction)tapExportButton:(id)sender
{
SendMailViewController *sendMailController = [[SendMailViewController alloc]init];
[self presentViewController:sendMailController animated:YES completion:^() {[sendMailController openMailDialog];}];
[sendMailController release];
}
SendMailViewController:
- (void)openMailDialog
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
...
[self presentModalViewController:mailer animated:YES];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
....
// Remove the mail view
// first i did this:
// [self dismissModalViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:^{[self removeFromParentViewController];}];
}
The problem is with the [self dismissViewControllerAnimated:YES completion:^{[self removeFromParentViewController];}]; in your didFinishWithResult method.
Remove that line and add the following line,
[controller dismissViewControllerAnimated:YES completion:^{[self dismissViewControllerAnimated:YES completion:nil]}];
That make sure we dismiss the controller after dismissing the MailController

Attempt to dismiss from view controller

I have an two class, in first class
- (IBAction)Login:(id)sender {
SampleViewController2 *sampleView = [[[SampleViewController2 alloc] init];
[self presentModalViewController:sampleView animated:YES];
}
in second class..
- (IBAction)Logout:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
it says warning Attempt to dismiss from view controller.. what is the problem here..
Add this:
if (![[self modalViewController] isBeingDismissed])
[self dismissModalViewControllerAnimated:YES];
Do a simple google search, the problem that you stated is common...
You might check it before dismissing if your current view's modal view is being dismissed or not
if (![[self modalViewController] isBeingDismissed]){
[self dismissModalViewControllerAnimated:YES];
}

Resources