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!!!