Reachability not working as expected

前端 未结 6 909
不思量自难忘°
不思量自难忘° 2021-02-19 02:53

Downloaded Reachability from Apple, using this method to check for an active connection:

-(BOOL)isReachable{

    Reachability *r = [Reachability reachabilityWit         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-19 03:17

    Use this code to check whether the device is connected to internet or not

    use this code in viewDidLoad :

     Reachability* internetReachable; = [Reachability reachabilityForInternetConnection];
        [internetReachable startNotifier];
    
        hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ;
        [hostReachable startNotifier];
    

    and add this function to your code:

    -(void) checkNetworkStatus:(NSNotification *)notice
    {
        recheabilityBool=FALSE;
        nonrecheabilityBool=FALSE;
        // called after network status changes
        NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
        switch (internetStatus)
        {
            case NotReachable:
            {
                nonrecheabilityBool=TRUE;
                internetCon=0;
                //NSLog(@"The internet is down.");
    
    
                break;
            }
            case ReachableViaWiFi:
            {
                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                internetCon=404;
                [prefs setInteger:internetCon forKey:@"conKey"];
    
                //NSLog(@"The internet is working via WIFI.");
                break;
    
            }
            case ReachableViaWWAN:
            {
                //NSLog(@"The internet is working via WWAN.");
    
                break;
            }
        }
    
        NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
        switch (hostStatus)
        {
            case NotReachable:
            {
                internetCon=0;
                if( nonrecheabilityBool==FALSE)
                {
                    //NSLog(@"A gateway to the host server is down.");
                }
                break;
    
            }
            case ReachableViaWiFi:
            {
                if(recheabilityBool==FALSE)
                {
    
                    recheabilityBool=TRUE;
    
                    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                    internetCon=404;
                    [prefs setInteger:internetCon forKey:@"conKey"];
    
    
                    //NSLog(@"The internet is working via WIFI.");
                    break;
                }
    
    
                //NSLog(@"A gateway to the host server is working via WIFI.");
    
                break;
            }
            case ReachableViaWWAN:
            {
                //NSLog(@"A gateway to the host server is working via WWAN.");
                break;
            }
        }
    }
    
    
    - (BOOL)connected
    {
        Reachability *reachability = [Reachability reachabilityForInternetConnection];
        NetworkStatus networkStatus = [reachability currentReachabilityStatus];
        return !(networkStatus == NotReachable);
    }
    

提交回复
热议问题