How to check if the user is connected to the Internet?

I have the following code:

let widget = await createWidget();
if (config.runsInWidget) {
  Script.setWidget(widget);
  
  widget.presentMedium();
  Script.complete();
}
​
async function createWidget() {
  let listWidget = new ListWidget();
  listWidget.backgroundColor = Color.white();
  
  let isConnected = await checkConnection();
  
  let widgetText = listWidget.addText("You are " + (isConnected ? "connected to the Internet." : "not connected to the Internet."));
  widgetText.font = Font.regularSystemFont(16);
  widgetText.textColor = new Color("212529", 1.0);
  widgetText.centerAlignText();
  
  return listWidget;
}
​
async function checkConnection() {
  /* I need help on this part! */
}

I need to find a way to check if the user is connected to the Internet. I even looked at the documentation various times, and still no luck.


Does anyone know a way to achieve this?

Try making a connection to http://icanhazip.com and see if get any response ?

There is another suggestion in this thread on the same topic.

Remember to search the forum for others asking the same question as well as reading the Scriptable documentation :wink:

Thank you, it works now! 😀 ❤