1

I have an issue with UIAlertView.

In my AppDelegate I check the reachability of the application: If it is not reachable I call the alert from Utils class.

- (void)reachabilityChanged:(NSNotification *)note
{
    Reachability* currentReachabilityObject = [note object];
    NSParameterAssert([currentReachabilityObject isKindOfClass:[Reachability class]]);
    NetworkStatus status = [currentReachabilityObject currentReachabilityStatus];

    if (status == NotReachable)
    {
        [Utils showAlert:@"NotReachableNetwork") title:@"Error")];
    }
}

And if I turn on/turn off Wi-Fi two-three times I get three alerts. But I want to show only one.

Please tell me how to check is there any alerts on the screen from AppDelegate.

Oleksandr Balabanov
  • 629
  • 1
  • 6
  • 30

2 Answers2

3

Why don't you keep a reference to the alert?

That way you just have to check if the alert is nil, if it is nil you can create a new alert. In case it isn't nil, it means you already have one showing and there's no need to show another. Easy as pie.

Flavio Silverio
  • 1,004
  • 8
  • 12
  • Well, it is a good solution. Can you just tell me an elegant implementation? How can i store this reference? – Oleksandr Balabanov Sep 24 '15 at 09:21
  • you can use a strong property, for example, you could declare this in your header: `@property (strong, nonatomic) UIAlertView *activeAlert;` and when creating your alertView, you would do `_activeAlert = alertYouJustCreated`. When dismissing the alert you can set the _activeAlert to nil. That way you can check if there's already an alert show with a simple `if(_activeAlert)` – Flavio Silverio Sep 24 '15 at 10:20
0

Please try below code and I think it will work for you.

#pragma mark - Internet Reachability Handlers -
- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
    NetworkStatus netStatus = [curReach currentReachabilityStatus];

    if (_changeReachability)
    {
        if(netStatus==NotReachable)
        {
           [Utils showAlert:@"NotReachableNetwork") title:@"Error")];
            _isNetAvailable = NO;
            _changeReachability = NO;
        }
        else
        {
            _isNetAvailable = YES;
            _changeReachability = NO;
        }
    }
}

//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
    _changeReachability = YES;
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self updateInterfaceWithReachability: curReach];
}

-(void)checkallTypesofInternet
{
    // For 3G Connection
    hostReach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    [hostReach startNotifier];
    [self updateInterfaceWithReachability: hostReach];

    // For Individual Net Connection
    internetReach = [Reachability reachabilityForInternetConnection];
    [internetReach startNotifier];
    [self updateInterfaceWithReachability: internetReach];

    // For WiFi
    wifiReach = [Reachability reachabilityForLocalWiFi];
    [wifiReach startNotifier];
    [self updateInterfaceWithReachability: wifiReach];
}

Let me know if you are still facing any issue.

Dipen
  • 268
  • 1
  • 14