This post will walk through testing the Connection.Connected property in your PowerApps.
Connection.Connected
If you are building a PowerApp with offline capabilities, then it becomes very important to test whether the device has an active internet connection. The sensor function Connection.Connected
can be used to test for connectivity – if true, you are connected, false not so much. A common scenario is to check the result of this function in an OnSelect formula. If connected, write or submit to a live data source, if disconnected, write to local collection for submittal later:
1 |
OnSelect = If(Connection.Connected, Patch(Contacts, Defaults(Contacts),{Name:"Adam"}), Collect(DraftContacts,{Name:"Adam"})) |
While developing your App, it’s somewhat difficult to test this in the web browser in PowerApps Studio, as the Connection.Connected function will usually return TRUE, even if you can’t connect to the internet (unplugged LAN cable, hotel Wifi without accepting terms of use, etc.). Even if you totally disable your network adapters, or put your computer in airplane mode, the property can still be unreliable. Sometimes it reports correctly, sometimes it takes a long time after changing connection status to update in the App, but usually just reports true.
Add to this the fact that if you are disconnected to test offline capabilities, then you can’t really make any changes and Save in PowerApps Studio, because, well, you are disconnected, right?
Fakin’ It
To get around this, you can fake it in the web browser. I call this Force Offline. It’s pretty simple to implement:
- Create a new screen, name it Debug, and put a button on it. This will be a hidden screen, since we won’t make any way for a user to navigate to it.
- In the button’s OnSelect(), set a global variable:
1Set(ForceOffline, !ForceOffline) - In the button’s Text property, change the label:
1If(ForceOffline, "Normal Connection Mode", "Force Offline Mode") - Now, wherever you reference
Connection.Connected
, wrap it in a check for ForceOffline mode first. Using the earlier example, you’d rewrite it like this:
OnSelect =
1If(If(ForceOffline, false, Connection.Connected), Patch(Contacts, Defaults(Contacts),{Name:"Adam"}), Collect(DraftContacts,{Name:"Adam"})
Now, when you want to test, just go to that screen in Edit mode, and Alt + Click the button and put it into Force Offline mode. You can now test away as-if your app is Offline, change it back and test Online, all without having to futz with Airplane mode or disabling/unplugging adapters!