Is there a way to check device’s network connectio...
# multiplatform
p
Is there a way to check device’s network connection in common code or we have to write platform specific code? Are there any working examples?
a
that would be interesting to see if there is..
wondering is kmp has access to the application level of the linux stack to get a handle on the network module.. can’t be sure
j
You have to write platform specific code, but you're free to expect/actual or abstract it behind an interface so that it's available in common. Also, there's no true way to check connectivity except to make a call. Nearly ever layer of the network stack is designed to lie to you about connectivity.
j
I would not recommend that after a cursory look
p
Let’s take the scenario below: “The device performs a call and the call fails with a http error, for example UnknownHostException.” • Can we safely assume that the device has no internet connection? Also: The main reason that we need this piece of info is to have a proper error handling.
j
Depends on whether you control every host that could possibly be connected to or not. And even then you're making assumptions about the availability of DNS and a pathway through the internet.
👍 1
p
Exactly! So we need to have the device’s network info. It would be really convenient if Ktor could throw a relevant exception out of the box. Btw: • the Ktor iOS client returns a message in Throwable and indicates that there is no internet connection. No error code though, just a message. • The Ktor okHttp client returns an UnknownHostException.
j
The network info doesn't provide this information. It is, at best, a guess. It doesn't deal with broken DNS, captive portals, and broken routes all of which will report a fully working connection. Moreover, even when a successful connection could be made the network info may report problems.
p
I see your point, so at best we should catch all connection related exceptions and display a general connection error to the user. We cannot accurately display a network specific error like: “The device appears to be offline. Make sure you have a connection and try again.” The initial need to check device’s network connection seems to be in the wrong direction. Thanks for your support! 🙂
j
You can react to an unknown host by checking and then using the "appears to be" wording. But just know that it might also be a captive portal or something else.
👍 1
1201 Views