Update Mac Network Detection Logic
Updated Mac Network Detection Logic to be on par with iOS
Initialized network cost before starting monitor, otherwise mac frequently reports unknown
@@ -84,7 +84,7 @@ namespace PAL_NS_BEGIN {
NetworkInformation::~NetworkInformation() noexcept
{
- if (@available(iOS 12.0, *))
+ if (@available(macOS 10.14, iOS 12.0, *))
{
if (m_isNetDetectEnabled)
{
@@ -105,7 +105,36 @@ namespace PAL_NS_BEGIN {
{
auto weak_this = std::weak_ptr<NetworkInformation>(shared_from_this());
- if (@available(iOS 12.0, *))
+ m_reach = [ODWReachability reachabilityForInternetConnection];
+ void (^block)(NSNotification*) = ^(NSNotification*)
+ {
+ auto strong_this = weak_this.lock();
+ if (!strong_this)
+ {
+ return;
+ }
+
+ // NetworkCost information is not available until iOS 12.
+ // Just make the best guess here.
+ switch (m_reach.currentReachabilityStatus)
+ {
+ case NotReachable:
+ strong_this->UpdateType(NetworkType_Unknown);
+ strong_this->UpdateCost(NetworkCost_Unknown);
+ break;
+ case ReachableViaWiFi:
+ strong_this->UpdateType(NetworkType_Wifi);
+ strong_this->UpdateCost(NetworkCost_Unmetered);
+ break;
+ case ReachableViaWWAN:
+ strong_this->UpdateType(NetworkType_WWAN);
+ strong_this->UpdateCost(NetworkCost_Metered);
+ break;
+ }
+ };
+ block(nil); // Update the initial status.
+
+ if (@available(macOS 10.14, iOS 12.0, *))
{
m_monitor = nw_path_monitor_create();
nw_path_monitor_set_queue(m_monitor, dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0));
@@ -136,7 +165,7 @@ namespace PAL_NS_BEGIN {
type = NetworkType_Wired;
}
cost = nw_path_is_expensive(path) ? NetworkCost_Metered : NetworkCost_Unmetered;
- if (@available(iOS 13.0, *))
+ if (@available(macOS 10.15, iOS 13.0, *))
{
if (nw_path_is_constrained(path))
{
@@ -157,37 +186,7 @@ namespace PAL_NS_BEGIN {
nw_path_monitor_cancel(m_monitor);
}
}
- else
- {
- m_reach = [ODWReachability reachabilityForInternetConnection];
- void (^block)(NSNotification*) = ^(NSNotification*)
- {
- auto strong_this = weak_this.lock();
- if (!strong_this)
- {
- return;
- }
-
- // NetworkCost information is not available until iOS 12.
- // Just make the best guess here.
- switch (m_reach.currentReachabilityStatus)
- {
- case NotReachable:
- strong_this->UpdateType(NetworkType_Unknown);
- strong_this->UpdateCost(NetworkCost_Unknown);
- break;
- case ReachableViaWiFi:
- strong_this->UpdateType(NetworkType_Wifi);
- strong_this->UpdateCost(NetworkCost_Unmetered);
- break;
- case ReachableViaWWAN:
- strong_this->UpdateType(NetworkType_WWAN);
- strong_this->UpdateCost(NetworkCost_Metered);
- break;
- }
- };
- block(nil); // Update the initial status.
- if (m_isNetDetectEnabled)
+ else if (m_isNetDetectEnabled)
{
m_notificationId =
[[NSNotificationCenter defaultCenter]
@@ -198,7 +197,6 @@ namespace PAL_NS_BEGIN {
[m_reach startNotifier];
}
}
- }
void NetworkInformation::UpdateType(NetworkType type) noexcept
{
