Notification when Internet became available on iOS
Asked Answered
M

2

6

Is it possible to receive a notification or something inside my app when the internet BECOMES available. I'm aware of reachability and all kinds of stuff. But what i want is to start some pending NSUrlConnections when internet becomes available on the device. Is there a easy way to do this, because I don't want to use a looping thread that checks constantly for reachability. Any suggestions?

Municipality answered 1/11, 2013 at 18:58 Comment(3)
From your question I don't understand why using Reachability and registering for ReachabilityChangedNotification s won't work. What is it you are looking for that Reachability doesn't offer?Colloquium
Reachability really is worth another look. You shouldn't be looping, but rather, like that code sample, call startNotifier and you'll get a notification when the Internet connectivity changes.Prentice
I don't agree this question is duplicate. Answer provided is about using Reachability, but question is about getting connection status update. As I've written in my answer, Reachability don't guarantee anything, it's more like helper. Suggesting to anyone, that Reachability is an right answer may lead to unhanded states in application!Pollinate
P
2

Ok, here is very nice post about Reachability: http://www.mikeash.com/pyblog/friday-qa-2013-06-14-reachability.html (check comments below!)
Tldr: You can fire block when your connection is back, but this solution is not perfect. There is no 100% reliable way to do this (except trying in loop), but you can try to mix those approaches.

Edit: Comment to @Jonah.at.GoDaddy answer:
Reachability can give you both connection notification errors: false positive and false negative (you can check it at WWDC 2011 session, I don’t remember which one; there are two about networking). So, my point is: you should never rely only on those notification. You can trigger refresh on status change, but there should be another way of doing it (user interaction, or some kind of active waiting).

Phonate answered 1/11, 2013 at 20:7 Comment(0)
A
0

Here is some code that I use...it may be a bit more than you need:

-(void)checkNetworkStatus
{
    // check for internet connection
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    // check if a pathway to a random host exists
    self.hostReachable = [Reachability reachabilityWithHostname:@"google.com"];
    [self.hostReachable startNotifier];
}


-(void) checkNetworkStatus:(NSNotification *)notice
{
    NetworkStatus hostStatus = [self.hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            DDLogInfo(@"A gateway to the host server is down.");
            if( self.canReachGoogle )
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle: LOCALIZED_NoInternetConnection
                                                            message: LOCALIZED_ConnectionNeeded
                                                           delegate:self cancelButtonTitle:LOCALIZED_Ok otherButtonTitles:nil];
                [alert show];
            }
            self.canReachGoogle = NO;
            break;
        }
        case ReachableViaWiFi:
        {
            DDLogInfo(@"A gateway to the host server is working via WIFI.");
            self.canReachGoogle = YES;

            break;
        }
        case ReachableViaWWAN:
        {
            DDLogInfo(@"A gateway to the host server is working via WWAN.");
            self.canReachGoogle = YES;

            break;
        }
    }
    DDLogInfo(@"Network connection has changed and is now: %@", self.canReachGoogle ? @"enabled" : @"disabled" );
}
Arise answered 1/11, 2013 at 21:33 Comment(1)
Yeah but that is relying on Google. Not the best approach. And also I asked for changes in reachability, not for reachability at a certain time.Municipality

© 2022 - 2024 — McMap. All rights reserved.