Skip to content

Problem when app moves to background #19

Open
dfrdmn opened this Issue Mar 3, 2011 · 9 comments

4 participants

@dfrdmn
dfrdmn commented Mar 3, 2011

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

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;

  • (void)hideRatingAlert { if (self.ratingAlert.visible) { [self.ratingAlert dismissWithClickedButtonIndex:-1 animated:NO]; }
    }

Then I tried using:

  • (void)appEnteredBackground { [[Appirater sharedInstance] hideRatingAlert]; }

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
dfrdmn commented Mar 3, 2011

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

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
Owner

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

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

@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

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

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

No problem... glad to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.