Hi Team,
We are connected our app in iOS device with the help of mobile hotspot that time we switched off the mobile network but still connected wifi. That time i need to trigger the event in Xamarin.iOS project. How to achieve this.
We tried two approach in Xamarin.iOS platform oriented but it will not triggered. I mentioned Reachability class and DeviceNetworkStatus class below that we tried. Please refer and give some solution.
In appdelegate.cs we called above class and tried to trigger.
private Reachability internetReachability;
private Reachability hostReachability;
public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
// Reachability class approach
//const string remoteHostName = "www.google.com";
// hostReachability = Reachability.ReachabilityWithHostName(remoteHostName);
//hostReachability.StartNotifier();
//internetReachability = Reachability.ReachabilityForInternetConnection();
//internetReachability.StartNotifier();
** // Device Network Status approach**
DependencyServiceImplementations.DeviceNetworkStatus.Instance.Start();
}
Note : Device Network Status approach we can able to triggered some mobile devices hotspot like settings available in AP Band width. Some more devices not triggered. Please kindly suggest good approach to handle my scenario.
DeviceNetworkStatus.cs
public class DeviceNetworkStatus
{
private static readonly Lazy networkStatus = new Lazy(() => new DeviceNetworkStatus());
private NWPathMonitor monitor;
public static DeviceNetworkStatus Instance => networkStatus.Value;
public void Start()
{
monitor = new NWPathMonitor();
var queue = DispatchQueue.GetGlobalQueue(DispatchQueuePriority.High);
monitor.SnapshotHandler = (NWPath path) =>
{
Debug.WriteLine(path.Status);
};
monitor.SetQueue(queue);
monitor.Start();
}
public void Stop()
{
if (monitor != null)
{
monitor.Cancel();
monitor.Dispose();
monitor = null;
}
}
}
Reachability.cs
public enum NetworkStatus
{
NotReachable,
ReachableViaWWAN,
ReachableViaWiFi
}
/// <summary>
/// Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
/// </summary>
public class Reachability : NSObject
{
~Reachability()
{
StopNotifier();
if (NetworkReachability != null)
{
NetworkReachability.Dispose();
}
}
public static string ReachabilityChangedNotification { get; } = "kNetworkReachabilityChangedNotification";
public NetworkReachability NetworkReachability { get; private set; }
public static Reachability ReachabilityWithHostName(string hostName)
{
var reachability = new NetworkReachability(hostName);
return new Reachability { NetworkReachability = reachability };
}
public static Reachability ReachabilityWithAddress(IPAddress hostAddress)
{
var reachability = new NetworkReachability(hostAddress);
return new Reachability { NetworkReachability = reachability };
}
//public static Reachability ReachabilityForInternetConnection()
//{
// var reachability = new NetworkReachability(new IPAddress(0));
// return new Reachability { NetworkReachability = reachability };
//}
public static Reachability ReachabilityForInternetConnection()
{
var reachability = new NetworkReachability("www.google.com");
return new Reachability { NetworkReachability = reachability };
}
private void HandleNotification(NetworkReachabilityFlags flags)
{
// Post a notification to notify the client that the network reachability changed.
NSNotificationCenter.DefaultCenter.PostNotificationName(ReachabilityChangedNotification, this);
}
#region Start and stop notifier
public bool StartNotifier()
{
var result = false;
var status = NetworkReachability.SetNotification(HandleNotification);
if (status == StatusCode.OK)
{
result = NetworkReachability.Schedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
}
return result;
}
private void StopNotifier()
{
if (NetworkReachability != null)
{
NetworkReachability.Unschedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
}
}
#endregion
#region Network Flag Handling
public NetworkStatus NetworkStatusForFlags(NetworkReachabilityFlags flags)
{
if ((flags & NetworkReachabilityFlags.Reachable) == 0)
{
// The target host is not reachable.
return NetworkStatus.NotReachable;
}
NetworkStatus result = NetworkStatus.NotReachable;
if ((flags & NetworkReachabilityFlags.ConnectionRequired) == 0)
{
// If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi...
result = NetworkStatus.ReachableViaWiFi;
}
if ((flags & NetworkReachabilityFlags.ConnectionOnDemand) != 0 ||
(flags & NetworkReachabilityFlags.ConnectionOnTraffic) != 0)
{
// ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs...
if ((flags & NetworkReachabilityFlags.InterventionRequired) == 0)
{
// ... and no [user] intervention is needed...
result = NetworkStatus.ReachableViaWiFi;
}
}
if ((flags & NetworkReachabilityFlags.IsWWAN) == NetworkReachabilityFlags.IsWWAN)
{
// ... but WWAN connections are OK if the calling application is using the CFNetwork APIs.
result = NetworkStatus.ReachableViaWWAN;
}
return result;
}
public bool ConnectionRequired()
{
if (NetworkReachability.TryGetFlags(out NetworkReachabilityFlags flags))
{
return (flags & NetworkReachabilityFlags.ConnectionRequired) != 0;
}
return false;
}
public NetworkStatus CurrentReachabilityStatus()
{
var returnValue = NetworkStatus.NotReachable;
if (NetworkReachability.TryGetFlags(out NetworkReachabilityFlags flags))
{
returnValue = this.NetworkStatusForFlags(flags);
}
return returnValue;
}
#endregion
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (NetworkReachability != null)
{
NetworkReachability.Dispose();
}
}
}
}