Reachability

DarioOrgazDarioOrgaz USMember ✭✭

Hi,

I've been working with reachability for long time. But today I notice that reachability changed stops to works.

Someone knows something about it?

I'm using Xlabs.Platform.Services.Reachability but their implementation is the following:

if (defaultRouteReachability == null)
        {
            defaultRouteReachability = new NetworkReachability(new IPAddress(0));
            defaultRouteReachability.SetNotification(OnChange);
            defaultRouteReachability.Schedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
        }

        return defaultRouteReachability.TryGetFlags(out flags) && IsReachableWithoutRequiringConnection(flags);

Answers

  • seanydaseanyda GBMember ✭✭✭✭✭

    As far as I'm aware xLabs is out of date. Have you checked out the Reachability Xamarin Docs?
    https://developer.xamarin.com/recipes/ios/network/reachability/detect_if_network_is_available/

  • DarioOrgazDarioOrgaz USMember ✭✭

    @seanyda said:
    As far as I'm aware xLabs is out of date. Have you checked out the Reachability Xamarin Docs?
    https://developer.xamarin.com/recipes/ios/network/reachability/detect_if_network_is_available/

    Yes, I know Xlabs is obsolete. For this reason I tried with native iOS code and no works.

  • BaberBaber PKMember ✭✭

    Hi Dario, check this out , I am using this class ,its a little customised version of reachablity framework of iOS . here is full code
    public enum NetworkStatus {
    NotReachable,
    ReachableViaCarrierDataNetwork,
    ReachableViaWiFiNetwork
    }

    public static class Reachability {
        public static string HostName = "www.google.com";
    
        public static bool IsReachableWithoutRequiringConnection(NetworkReachabilityFlags flags) {
            // Is it reachable with the current network configuration?
            bool isReachable = (flags & NetworkReachabilityFlags.Reachable) != 0;
    
            // Do we need a connection to reach it?
            bool noConnectionRequired = (flags & NetworkReachabilityFlags.ConnectionRequired) == 0
                || (flags & NetworkReachabilityFlags.IsWWAN) != 0;
    
            return isReachable && noConnectionRequired;
        }
    
        /// <summary>
        /// Baber: I am adding over load for default host
        /// </summary>
        /// <returns><c>true</c>, if host reachable was ised, <c>false</c> otherwise.</returns>
    
        public static bool IsHostReachable()
        {
            if (string.IsNullOrEmpty(HostName))
                return false;
    
            using (var r = new NetworkReachability(HostName))
            {
                NetworkReachabilityFlags flags;
    
                if (r.TryGetFlags(out flags))
                    return IsReachableWithoutRequiringConnection(flags);
            }
            return false;
        }
        // Is the host reachable with the current network configuration
        public static bool IsHostReachable(string host) {
            if (string.IsNullOrEmpty(host))
                return false;
    
            using (var r = new NetworkReachability(host)) {
                NetworkReachabilityFlags flags;
    
                if (r.TryGetFlags(out flags))
                    return IsReachableWithoutRequiringConnection(flags);
            }
            return false;
        }
    
        //
        // Raised every time there is an interesting reachable event,
        // we do not even pass the info as to what changed, and
        // we lump all three status we probe into one
        //
        public static event EventHandler ReachabilityChanged;
    
        static void OnChange(NetworkReachabilityFlags flags) {
            var h = ReachabilityChanged;
            if (h != null)
                h(null, EventArgs.Empty);
        }
    
        //
        // Returns true if it is possible to reach the AdHoc WiFi network
        // and optionally provides extra network reachability flags as the
        // out parameter
        //
        static NetworkReachability adHocWiFiNetworkReachability;
    
        public static bool IsAdHocWiFiNetworkAvailable(out NetworkReachabilityFlags flags) {
            if (adHocWiFiNetworkReachability == null) {
                adHocWiFiNetworkReachability = new NetworkReachability(new IPAddress(new byte[] { 169, 254, 0, 0 }));
                adHocWiFiNetworkReachability.SetNotification(OnChange);
                adHocWiFiNetworkReachability.Schedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
            }
    
            return adHocWiFiNetworkReachability.TryGetFlags(out flags) && IsReachableWithoutRequiringConnection(flags);
        }
    
        static NetworkReachability defaultRouteReachability;
    
        static bool IsNetworkAvailable(out NetworkReachabilityFlags flags) {
            if (defaultRouteReachability == null) {
                defaultRouteReachability = new NetworkReachability(new IPAddress(0));
                defaultRouteReachability.SetNotification(OnChange);
                defaultRouteReachability.Schedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
            }
            return defaultRouteReachability.TryGetFlags(out flags) && IsReachableWithoutRequiringConnection(flags);
        }
    
        static NetworkReachability remoteHostReachability;
    
        public static NetworkStatus RemoteHostStatus() {
            NetworkReachabilityFlags flags;
            bool reachable;
    
            if (remoteHostReachability == null) {
                remoteHostReachability = new NetworkReachability(HostName);
    
                // Need to probe before we queue, or we wont get any meaningful values
                // this only happens when you create NetworkReachability from a hostname
                reachable = remoteHostReachability.TryGetFlags(out flags);
    
                remoteHostReachability.SetNotification(OnChange);
                remoteHostReachability.Schedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
            } else {
                reachable = remoteHostReachability.TryGetFlags(out flags);
            }
    
            if (!reachable)
                return NetworkStatus.NotReachable;
    
            if (!IsReachableWithoutRequiringConnection(flags))
                return NetworkStatus.NotReachable;
    
            return (flags & NetworkReachabilityFlags.IsWWAN) != 0 ?
                NetworkStatus.ReachableViaCarrierDataNetwork : NetworkStatus.ReachableViaWiFiNetwork;
        }
    
        public static NetworkStatus InternetConnectionStatus() {
            NetworkReachabilityFlags flags;
            bool defaultNetworkAvailable = IsNetworkAvailable(out flags);
            if (defaultNetworkAvailable && ((flags & NetworkReachabilityFlags.IsDirect) != 0))
                return NetworkStatus.NotReachable;
            else if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
                return NetworkStatus.ReachableViaCarrierDataNetwork;
            else if (flags == 0)
                return NetworkStatus.NotReachable;
            return NetworkStatus.ReachableViaWiFiNetwork;
        }
    
        public static NetworkStatus LocalWifiConnectionStatus() {
            NetworkReachabilityFlags flags;
            if (IsAdHocWiFiNetworkAvailable(out flags))
                if ((flags & NetworkReachabilityFlags.IsDirect) != 0)
                    return NetworkStatus.ReachableViaWiFiNetwork;
    
            return NetworkStatus.NotReachable;
        }
    

    you can use it in many ways , for simply using it just call
    if (Reachability.InternetConnectionStatus() == NetworkStatus.NotReachable)
    retur false;

  • BaberBaber PKMember ✭✭

    Hi Dario, check this out , I am using this class ,its a little customised version of reachablity framework of iOS . here is full code
    public enum NetworkStatus {
    NotReachable,
    ReachableViaCarrierDataNetwork,
    ReachableViaWiFiNetwork
    }

    public static class Reachability {
        public static string HostName = "www.google.com";
    
        public static bool IsReachableWithoutRequiringConnection(NetworkReachabilityFlags flags) {
            // Is it reachable with the current network configuration?
            bool isReachable = (flags & NetworkReachabilityFlags.Reachable) != 0;
    
            // Do we need a connection to reach it?
            bool noConnectionRequired = (flags & NetworkReachabilityFlags.ConnectionRequired) == 0
                || (flags & NetworkReachabilityFlags.IsWWAN) != 0;
    
            return isReachable && noConnectionRequired;
        }
    
        /// <summary>
        /// Baber: I am adding over load for default host
        /// </summary>
        /// <returns><c>true</c>, if host reachable was ised, <c>false</c> otherwise.</returns>
    
        public static bool IsHostReachable()
        {
            if (string.IsNullOrEmpty(HostName))
                return false;
    
            using (var r = new NetworkReachability(HostName))
            {
                NetworkReachabilityFlags flags;
    
                if (r.TryGetFlags(out flags))
                    return IsReachableWithoutRequiringConnection(flags);
            }
            return false;
        }
        // Is the host reachable with the current network configuration
        public static bool IsHostReachable(string host) {
            if (string.IsNullOrEmpty(host))
                return false;
    
            using (var r = new NetworkReachability(host)) {
                NetworkReachabilityFlags flags;
    
                if (r.TryGetFlags(out flags))
                    return IsReachableWithoutRequiringConnection(flags);
            }
            return false;
        }
    
        //
        // Raised every time there is an interesting reachable event,
        // we do not even pass the info as to what changed, and
        // we lump all three status we probe into one
        //
        public static event EventHandler ReachabilityChanged;
    
        static void OnChange(NetworkReachabilityFlags flags) {
            var h = ReachabilityChanged;
            if (h != null)
                h(null, EventArgs.Empty);
        }
    
        //
        // Returns true if it is possible to reach the AdHoc WiFi network
        // and optionally provides extra network reachability flags as the
        // out parameter
        //
        static NetworkReachability adHocWiFiNetworkReachability;
    
        public static bool IsAdHocWiFiNetworkAvailable(out NetworkReachabilityFlags flags) {
            if (adHocWiFiNetworkReachability == null) {
                adHocWiFiNetworkReachability = new NetworkReachability(new IPAddress(new byte[] { 169, 254, 0, 0 }));
                adHocWiFiNetworkReachability.SetNotification(OnChange);
                adHocWiFiNetworkReachability.Schedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
            }
    
            return adHocWiFiNetworkReachability.TryGetFlags(out flags) && IsReachableWithoutRequiringConnection(flags);
        }
    
        static NetworkReachability defaultRouteReachability;
    
        static bool IsNetworkAvailable(out NetworkReachabilityFlags flags) {
            if (defaultRouteReachability == null) {
                defaultRouteReachability = new NetworkReachability(new IPAddress(0));
                defaultRouteReachability.SetNotification(OnChange);
                defaultRouteReachability.Schedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
            }
            return defaultRouteReachability.TryGetFlags(out flags) && IsReachableWithoutRequiringConnection(flags);
        }
    
        static NetworkReachability remoteHostReachability;
    
        public static NetworkStatus RemoteHostStatus() {
            NetworkReachabilityFlags flags;
            bool reachable;
    
            if (remoteHostReachability == null) {
                remoteHostReachability = new NetworkReachability(HostName);
    
                // Need to probe before we queue, or we wont get any meaningful values
                // this only happens when you create NetworkReachability from a hostname
                reachable = remoteHostReachability.TryGetFlags(out flags);
    
                remoteHostReachability.SetNotification(OnChange);
                remoteHostReachability.Schedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
            } else {
                reachable = remoteHostReachability.TryGetFlags(out flags);
            }
    
            if (!reachable)
                return NetworkStatus.NotReachable;
    
            if (!IsReachableWithoutRequiringConnection(flags))
                return NetworkStatus.NotReachable;
    
            return (flags & NetworkReachabilityFlags.IsWWAN) != 0 ?
                NetworkStatus.ReachableViaCarrierDataNetwork : NetworkStatus.ReachableViaWiFiNetwork;
        }
    
        public static NetworkStatus InternetConnectionStatus() {
            NetworkReachabilityFlags flags;
            bool defaultNetworkAvailable = IsNetworkAvailable(out flags);
            if (defaultNetworkAvailable && ((flags & NetworkReachabilityFlags.IsDirect) != 0))
                return NetworkStatus.NotReachable;
            else if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
                return NetworkStatus.ReachableViaCarrierDataNetwork;
            else if (flags == 0)
                return NetworkStatus.NotReachable;
            return NetworkStatus.ReachableViaWiFiNetwork;
        }
    
        public static NetworkStatus LocalWifiConnectionStatus() {
            NetworkReachabilityFlags flags;
            if (IsAdHocWiFiNetworkAvailable(out flags))
                if ((flags & NetworkReachabilityFlags.IsDirect) != 0)
                    return NetworkStatus.ReachableViaWiFiNetwork;
    
            return NetworkStatus.NotReachable;
        }
    

    you can use it in many ways , for simply using it just call
    if (Reachability.InternetConnectionStatus() == NetworkStatus.NotReachable)
    retur false;

  • DarioOrgazDarioOrgaz USMember ✭✭

    @Baber Sorry for the delay.

    The code that I have is the same as you. But, with iOS 11, the ReachabilityChanged event is raised when network changes?

    Thank you!!

Sign In or Register to comment.