Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 42 additions & 39 deletions lib/pal/posix/NetworkInformationImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -103,37 +103,6 @@ virtual NetworkCost GetNetworkCost()

void NetworkInformation::SetupNetDetect()
{
auto weak_this = std::weak_ptr<NetworkInformation>(shared_from_this());

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();
Expand Down Expand Up @@ -186,15 +155,49 @@ virtual NetworkCost GetNetworkCost()
nw_path_monitor_cancel(m_monitor);
}
}
else if (m_isNetDetectEnabled)
else
{
m_notificationId =
[[NSNotificationCenter defaultCenter]
addObserverForName: kNetworkReachabilityChangedNotification
object: nil
queue: nil
usingBlock: block];
[m_reach startNotifier];
auto weak_this = std::weak_ptr<NetworkInformation>(shared_from_this());

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)
{
m_notificationId =
[[NSNotificationCenter defaultCenter]
addObserverForName: kNetworkReachabilityChangedNotification
object: nil
queue: nil
usingBlock: block];
[m_reach startNotifier];
}
}
}

Expand Down
Loading