How to return BOOL when checking internet connection in XCODE

前端 未结 4 1139
自闭症患者
自闭症患者 2021-01-23 03:52

I want to be able to check for internet connectivity when my View loads. To predetermine the contents of my view.

I have the following viewDidLoad method:



        
4条回答
  •  孤独总比滥情好
    2021-01-23 03:54

    Best way is to use Reachability code. Check here for apple sample code. That has a lot of convenience methods to check internet availability, Wifi/WAN connectivity check etc..

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name:kReachabilityChangedNotification object:nil];
    
    reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];
    
    - (void)networkChanged:(NSNotification *)notification
    {
    
      NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
    
      if(remoteHostStatus == NotReachable) { NSLog(@"not reachable");}
      else if (remoteHostStatus == ReachableViaWiFiNetwork) { NSLog(@"wifi"); }
      else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { NSLog(@"carrier"); }
    }
    

提交回复
热议问题