1

I am making an app that should support iOS versions from iOS5 onwards. It uses a UIAlertView, and if it's visible when the user presses the home button, I would like it to be dismissed before the user returns to the app (i.e. it's gone when the app is re-opened using multitasking). All methods in the app delegate show it as not visible (isVisible=NO) even if it is still visible when reopened. Is there a way to do this?

Thanks.

0

2 Answers 2

7

Or you inherit your class from UIAlertView and add NSNotification observer for UIApplicationWillResignActiveNotification and when notification occurs call to alertview method dismissWithClickedButtonIndex:

Example: .h file

#import <UIKit/UIKit.h>

@interface ADAlertView : UIAlertView

@end

.m file

#import "ADAlertView.h"

@implementation ADAlertView

- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id) initWithTitle:(NSString *)title
             message:(NSString *)message
            delegate:(id)delegate
   cancelButtonTitle:(NSString *)cancelButtonTitle
   otherButtonTitles:(NSString *)otherButtonTitles, ... {
    self = [super initWithTitle:title
                        message:message
                       delegate:delegate
              cancelButtonTitle:cancelButtonTitle
              otherButtonTitles:otherButtonTitles, nil];

    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
             selector:@selector(dismiss:)
                 name:UIApplicationDidEnterBackgroundNotification
               object:nil];
    }

    return self;
}

- (void) dismiss:(NSNotification *)notication {
    [self dismissWithClickedButtonIndex:[self cancelButtonIndex] animated:YES];
}

@end

With your own class inherited from UIAlertView you are need not to store link to alertview or something else, only one thing that you must do its replace UIAlertView to ADAlertView (or any other class name). Feel free to use this code example (if you are not using ARC, you should add to the dealloc method [super dealloc] after [[NSNotificatioCenter defaultCenter] removeObserver:self])

4
  • This might work, but according to the documentation "The UIAlertView class is intended to be used as-is and does not support subclassing".
    – Martin R
    Feb 21, 2013 at 17:54
  • anyway this is better, that store a link to all alerts, that you need to dissmiss, when application enters background Feb 21, 2013 at 17:55
  • 1
    @MartinR I believe his type of subclassing is "safe". I think the intent of the note in the docs about subclassing is to keep people from subclassing for the purpose of modifying the user interface or doing other non-safe mucking about.
    – rmaddy
    Feb 21, 2013 at 18:03
  • Tried different ways to dismiss the alert (willDisAppear:, etc) and they didn't work. This one made my life a bit easier, rather than coming up to this myself ;) Oct 9, 2013 at 12:26
4

Keep a reference to the displayed UIAlertView in your app delegate. When you show the alert, set the reference; when the alert is dismissed, nil out the reference.

In your app delegate's applicationWillResignActive: or applicationDidEnterBackground: method, call the dismissWithClickedButtonIndex:animated: method on the reference to the alert view. This would take care of dismissing it on pressing the "home" button.

Keep in mind that applicationWillResignActive: will be called for things such as phone calls, so you need to decide if you'd like to dismiss the alert in cases like that or if you should keep it up through the phone call.

3
  • 3
    I think that applicationWillResignActive is also called for temporary interruptions such as incoming phone calls or SMS messages, so applicationDidEnterBackground might be a better choice.
    – Martin R
    Feb 21, 2013 at 17:48
  • @MartinR Thank you very much for a great comment! I edited the answer to reflect it. Feb 21, 2013 at 18:00
  • This, but definitely use applicationDidEnterBackground!
    – Hyperbole
    Feb 21, 2013 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.