Notification when Internet became available on iOS

爷,独闯天下 提交于 2019-12-01 03:49:59

问题


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?


回答1:


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).




回答2:


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" );
}


来源:https://stackoverflow.com/questions/19733536/notification-when-internet-became-available-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!