How to detect loss of internet reachability when wifi source loses connection?

后端 未结 3 1630
日久生厌
日久生厌 2021-02-04 16:10

I am trying hard to get the authentic internet connectivity status. I have used Apple\'s Reachability but it\'s only giving me the status of wifi connectivity i.e. not covering

3条回答
  •  时光取名叫无心
    2021-02-04 16:50

    I was getting too the Wifi flag even with it disconnected. There is a bug in the Reachability.m method:

    - (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
    

    it uses a BOOL as return value, but it assigns to it the values of a struct with 3 values:

    typedef enum : NSInteger {
        NotReachable = 0,
        ReachableViaWiFi,
        ReachableViaWWAN
    } NetworkStatus;
    

    So if whether Wifi or Cellular are available, it will ReachableViaWiFi (as a BOOL can be 0 or 1, but not two)

    To fix it, just change in the method above this:

    BOOL returnValue = NotReachable;
    

    For this:

    int returnValue = NotReachable;
    

    And you're good to go. Hope it helps.

提交回复
热议问题