How to use reachability class to detect valid internet connection?

前端 未结 7 640
遇见更好的自我
遇见更好的自我 2020-11-29 23:24

I\'m new to iOS development and am struggling to get the reachability.h class to work. Here is my code for view controller:

- (void)viewWillAppear:(BOOL)anim         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 23:59

    just Import Reachability classes and after that

    -(BOOL) connectedToNetwork
     {
    const char *host_name = "www.google.com";
      BOOL _isDataSourceAvailable = NO;
    Boolean success;
    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,host_name);
    SCNetworkReachabilityFlags flags;
    success = SCNetworkReachabilityGetFlags(reachability, &flags);
    _isDataSourceAvailable = success &&
    (flags & kSCNetworkFlagsReachable) &&
    !(flags & kSCNetworkFlagsConnectionRequired);
    
    CFRelease(reachability);
    
    return _isDataSourceAvailable;
    }
    

提交回复
热议问题