Created
May 13, 2024 14:38
-
-
Save andremion/4d58c5219d00a993eeec82ad47eb4773 to your computer and use it in GitHub Desktop.
iOS connectivity status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalForeignApi::class) | |
class ConnectivityStatus { | |
private val _isConnected = MutableStateFlow(false) | |
val isConnected get() = _isConnected.value | |
init { | |
val monitor = nw_path_monitor_create() | |
nw_path_monitor_set_queue( | |
monitor, | |
dispatch_get_global_queue( | |
QOS_CLASS_BACKGROUND.convert(), | |
0.convert(), | |
), | |
) | |
nw_path_monitor_set_update_handler(monitor) { | |
val status = nw_path_get_status(it) | |
val label = | |
when (status) { | |
nw_path_status_satisfiable -> "Satisfiable" | |
nw_path_status_invalid -> "Invalid" | |
nw_path_status_satisfied -> "Satisfied" | |
nw_path_status_unsatisfied -> "Unsatisfied" | |
else -> "Unknown" | |
} | |
println("ConnectivityStatus: $status / $label") | |
_isConnected.value = status == nw_path_status_satisfied | |
} | |
nw_path_monitor_start(monitor) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment