Search code examples
iosobjective-cxcodenetwork-programmingalertview

AlertView dismiss on its own (do not want this) Xcode


I am trying to check if the device is connected to the internet. If it doesn't, an alert is going to pop up and the user need to press 'try again' to try again. In my code, the first time the alert view pop up, I press 'try again' and then the second time the alert pop up, it dismiss after one second on its own. But it is suppose to only dismiss when I press 'try again'.

Here's my code:

In .h:

#import <UIKit/UIKit.h>

@class Reachability;

@interface ViewController : UIViewController{
    Reachability* internetReachable;
    Reachability* hostReachable;

    UIAlertView *networkAlert;
}

-(void) checkNetworkStatus;

@end

In .m:

#import "ViewController.h"
#import "Reachability.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    networkAlert = [[UIAlertView alloc]initWithTitle: @"Unstable Network Connection"
                                         message: @"Unstable network connection."
                                        delegate: self
                               cancelButtonTitle: nil
                               otherButtonTitles: @"Try Again", nil];

    [self checkNetworkStatus];

 }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


 -(void) checkNetworkStatus
{
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
         {
            NSLog(@"The internet is down.");
            [networkAlert show];
            break;
         }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");        
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");

            break;
        }
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        NSLog(@"user pressed Button Indexed 0");
        // Any action can be performed here
        [self checkNetworkStatus];

    }
}

@end

Sorry for my english and Thank you so much in advance!!!


Solution

  • I was able to replicate the issue you're seeing, and it has to do with the fact that you are essentially telling the alertView to show when it is still on screen and in the process of being dismissed. There is a small delay in the time it takes for the alertView to be dismissed, and when they hit the Try Again button you are both dismissing and presenting the same alert view.

    The fix for this is remove the alert view variable you have, there is really no need to hold on to it in this case. Use a simple method to present the message, and call it whenever you want, and because it is a separate instance of an alert each time, you don't have to worry about showing/hiding it at the same time.

    - (void)showInternetConnectionMessage {
        [[[UIAlertView alloc]initWithTitle: @"Unstable Network Connection" message: @"Unstable network connection." delegate: self cancelButtonTitle: nil otherButtonTitles: @"Try Again", nil] show];
    }