https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
p

ptsiogas

02/02/2021, 5:01 PM
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

Arkangel

02/02/2021, 5:04 PM
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

jw

02/02/2021, 6:50 PM
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

jw

02/03/2021, 2:32 AM
I would not recommend that after a cursory look
p

ptsiogas

02/03/2021, 9:14 AM
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

jw

02/03/2021, 2:01 PM
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

ptsiogas

02/03/2021, 2:13 PM
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

jw

02/03/2021, 2:14 PM
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

ptsiogas

02/03/2021, 2:24 PM
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

jw

02/03/2021, 3:32 PM
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
375 Views