arashpayan / appirater

A utility that reminds your iPhone app's users to review the app.
http://arashpayan.com/blog/2009/09/07/presenting-appirater/
4.62k stars 856 forks source link

Problem when app moves to background #19

Closed dfrdmn closed 7 years ago

dfrdmn commented 13 years ago

If the UIAlertView is present and the user moves the app to the background, Appirater gets stuck in a vicious circle of briefly presenting the alert and then dismissing it.

As a brute force measure, I am simply cycling through subviews from applicationDidEnterBackground: and calling dismissWithClickedButtonIndex: on the UIAlertView. This works for my particular implementation, but is obviously not safe for everyone.

meachware commented 13 years ago

I found a possible solution in the GKTank sample, a UIAlert has a visible property but I'm having troubles properly implementing into Appirater

In Appirater.h UIAlertView *ratingAlert; }

@property(nonatomic, retain) UIAlertView *ratingAlert;

In Appirater.m

// in showRatingAlert add self.ratingAlert = alertView;

Then I tried using:

Update: now compiles without warnings... the goal would be to add: [Appirater appEnteredBackground]; inside the appDelegate, didEnterBackground

Edit: I don't see the cycling problem, in the simulator iOS 4.2.

What iOS version did you see the problem on?

dfrdmn commented 13 years ago

What warnings are you getting?

Instead of having to add a line in applicationDidEnterBackground:, why not set up a notification? This would keep Appirater more modular.

- (void)showRatingAlert {
    // current code
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideRatingAlert) name:@"UIApplicationDidEnterBackgroundNotification" object:nil];
}

Then, of course, remove the observer:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIApplicationDidEnterBackgroundNotification" object:nil];
    // current code
}
meachware commented 13 years ago

Well that should work very slick.... nice

Whoops, what iOS version did you see the problem on? Edit: My nslog is not being displayed upon enter background; again in the simulator.

Edit: Okay tested on my iPod and can see the repeat cycle, if you suspend the app 5 times (with debug ON) you eventually have to dismiss the alert 5 times. I changed the notification type to: UIApplicationWillResignActiveNotification I have tested it on my iPod and it is now calling the hide alert method upon suspend and regardless of the number of times I suspend, when I dismiss the alert (No Thanks button) it goes away.

arashpayan commented 13 years ago

It might be worth registering the Appirater class for UIApplicationWillResignActiveNotification when it's initialized. Then if the notification comes in while it's showing an alert, it knows to dismiss the alert on it's own. I need to double check the UIAlertView delegate to make sure it handles the programmatic dismissal properly, but that shouldn't be a big deal.

meachware commented 13 years ago

Yup, sorry didn't update the post. I have been testing it on several apps with it loading the notification in the init method and not removing it. At one point I think I went through 20 sleeps/locks in debug mode and every time the alert popped back up and the final time I acknowledged it, it went away properly. I forked the project but since I just learned how to spell GIT about two days ago it hasn't been that high on my to-do list, sorry

meachware commented 13 years ago

@arashpayan - sorry that I'm having to cut my "git" teeth on your project. Using Xcode 4 I successfully cloned my fork, updated the files and pushed them back (yeah for me!). I'm extremely hesitant to submit a pull request, well for fear of actually breaking github AND the entire interweb :-0. I'm fairly sure I won't blow up the planet, so I guess I'll submit the pull request for your to review. Again sorry for my noobi-ness :-/

bobthedino commented 13 years ago

Correct me if I'm wrong, but in this version it seems that the property "ratingAlert" never gets anything assigned to it, so the code to dismiss the alert when the app goes into the background doesn't do anything?

meachware commented 13 years ago

THANKS! You are not wrong - I just submitted the fix This code: self.ratingAlert = alertView; was added to showAlert method.

I'll submit the pull request, thanks again!

bobthedino commented 13 years ago

No problem... glad to help!