Network connectivity

Connectivity information describes the network connection available to a device, such as Wi-Fi or cellular. It helps an app explain connection changes and choose when to attempt network work; it does not guarantee that a particular service is reachable.

day-part-connectivity returns a connectivity snapshot. For sending requests, use HTTP.

Add to your app

Add day-part-connectivity to your app:

cargo add 'day-part-connectivity@0.*'

The implementation is selected by the compilation target; no toolkit feature flags or dependency entry in Day.toml are needed.

Platforms / toolkitsAvailability
macOS, iOS, Android, Windows, Linux (GTK / Qt), HarmonyOSConnectivity information; available detail varies by OS.
WebBrowser data-saving preference where available. status() returns None.

Authoring

if let Some(n) = day_part_connectivity::status() {
    println!("online: {}, kind: {:?}, expensive: {:?}", n.online, n.kind, n.expensive);
}

status() -> Option<NetworkStatus> returns None where there’s no connectivity API (or the reading failed), which is distinct from a successful reading that says offline (Some with online: false). NetworkStatus { online: bool, kind: NetworkKind, expensive: Option<bool> }; NetworkKind is Wifi | Cellular | Ethernet | Other | None.

There are no crate features; platform selection is purely #[cfg(target_os)], because connectivity is an OS concern rather than a widget-toolkit one. crates/day-part-connectivity/examples/network.rs is a plain main that uses it with no Day framework at all.

Platform implementations

Platform guidance: Android: network operations; Apple: URL loading system.

OSAPIdependency
macOSSCNetworkReachability (SystemConfiguration)raw C FFI, shared apple.rs
iOSSCNetworkReachability (IsWWAN ⇒ cellular)raw C FFI, shared apple.rs
WindowsGetNetworkConnectivityHint (iphlpapi, resolved dynamically)raw FFI (kernel32 + runtime lookup)
Linux/sys/class/net interface scanoptional system D-Bus cost query
HarmonyOSOH_NetConn_HasDefaultNet / GetNetCapabilities (libnet_connection.so)raw FFI (NetworkKit)
AndroidConnectivityManager + NetworkCapabilities via a Java shimday-toolkit-material + [package.metadata.day.android]

Cost and data saving

network_conditions() returns two independent optional booleans in NetworkConditions. expensive describes metering or cost. constrained describes a request to reduce data usage. Either value can be unknown. A snapshot describes the default route at the time of observation.

PlatformExpensiveConstrained
macOS and iOSNetwork path costLow Data Mode. The first snapshot can be unknown while the path monitor starts.
AndroidActive network capabilities, including temporary unmeteringData Saver applies to this app on a metered network. App exemptions are respected.
WindowsFixed or variable connection costBackground data usage restriction on the current internet connection profile.
LinuxNetworkManager metering, including estimated cost, when libsystemd and the service are availableUnknown
HarmonyOSNetworkKit metering capabilityUnknown
WebUnknownBrowser data-saving preference, when exposed

What each platform can report

Every field is best-effort; the platforms report different amounts of detail:

  • macOS/iOS: reachability answers “could traffic to the default route flow right now?” from the routing table. It sends no packets, so it cannot detect a captive portal or a dead upstream: online means routable, not verified internet. The only transport bit is IsWWAN (iOS-only): cellular reports Cellular; any other reachable iOS connection reports Wifi (the classic “ReachableViaWiFi” reading; it could in fact be wired or a tether). macOS gets no transport info at all, so an online Mac reports Other.
  • Android: the richest reading. online is the system’s INTERNET + VALIDATED verdict for the active network, kind comes from TRANSPORT_WIFI/CELLULAR/ETHERNET, and expensive is the inverse of the unmetered and temporarily unmetered capabilities. Requires android.permission.ACCESS_NETWORK_STATE, a normal install-time permission the crate contributes to the manifest itself (see below).
  • HarmonyOS: online = a default network is activated; kind from the bearer type, expensive from NETCONN_NET_CAPABILITY_NOT_METERED. The app must declare ohos.permission.GET_NETWORK_INFO in its module.json5 (requestPermissions; normal permission, no prompt) or the calls fail with 201 and status() returns None.
  • Linux: no daemon-independent connectivity API is guaranteed (NetworkManager is optional), so the crate scans /sys/class/net: online = a non-loopback interface has operstate up (link-level, not validated internet); kind from the kernel’s predictable name prefixes (wl* wireless, en*/eth* wired, ww* wwan), preferring wired > wifi > cellular when several are up; expensive comes from NetworkManager where available.
  • Windows: GetNetworkConnectivityHint (Windows 10 2004+) gives a connectivity level (online = internet or constrained-internet access) and a cost (expensive) but no transport, so kind is Other when online. The symbol is resolved at runtime via LoadLibrary/GetProcAddress, so apps still start on older Windows; status() returns None there.

A snapshot is a point-in-time poll; a change-notification rail (SCNetworkReachability callbacks, Android NetworkCallback, OH_NetConn_RegisterNetConnCallback, NotifyNetworkConnectivityHintChange) is a v2 follow-up.

Connectivity says whether traffic could flow; to actually move bytes through the platform’s HTTP stack (proxies, VPN, platform TLS), see its sibling part day-part-http.

Extension integration

Like day-part-device, this is a headless external crate: it has no UI Piece and registers nothing into any toolkit’s RENDERERS slice. It additionally exercises the manifest-permission overlay (from the webview work, docs/extending): [package.metadata.day.android] stages its own Java shim and contributes android.permission.ACCESS_NETWORK_STATE, which day build merges into the app manifest with no changes to any core Day crate. On Android the crate rides on the Day runtime (day-toolkit-material’s cached JVM + DayBridge.ctx); on every other platform it is fully day-independent.