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.

Alex Balabanov
  • 589
  • 1
  • 5
  • 30
  • 1
    http://stackoverflow.com/questions/21179922/can-i-check-if-any-uialertview-displaying-right-now – Kirit Modi Sep 24 '15 at 07:55
  • TY. I will check it. – Alex Balabanov Sep 24 '15 at 08:24
  • 1
    Why don't you just store a boolean on the instance that is true if you have shown the alert, then check if before showing it again? This would be much more future proof than trying to determine if a `UIAlertView` is displaying, remember `UIAlertView` is deprecated and being replaced by `UIAlertController`. – Steve Wilford Sep 24 '15 at 08:38
  • But how can I set this BOOL flag to NO? I can't do it. – Alex Balabanov Sep 24 '15 at 09:20

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
  • 934
  • 8
  • 11
  • Well, it is a good solution. Can you just tell me an elegant implementation? How can i store this reference? – Alex 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