Skip to content

iOS Tutorial: Check Internet Connection With NWPathMonitor

Traditionally, you will use a third party library to detect the internet connection for the device. With Apple native’s framework NWPathMonitor, you will be able to do the same thing as well.

The supported version is iOS 12.0+, macOS 10.14+, watchOS 5.0+. If you do not have any user using older version, this would be an ideal solution for you.

An observer that you use to monitor and react to network changes.

Apple Documentation

In this tutorial, you’ll learn how to use NWPathMonitor. You’ll learn:

  • How to detect network connection with NWPathMonitor.

Prerequisites

To follow along this tutorial, you’ll need some basic knowledge in:

  • A basic familiarity with Swift.
  • At least Xcode 10.0+

NWPathMonitor

To start, you should import the library.

With that you will be able to create an instance of NWPathMonitor. Also, by adding an additional of DispatchQueue for the monitor to run.

To know if the internet is connected, use pathUpdateHandler. Here, you will call the start so that you know if the connection status changes.

Remember to test it on your real device for this to work. Testing on simulator will get a half-baked result.

Where to go From Here

If you feel like you are ready to take on more challenges, feel free to check out some other tutorials that we have created:

Published iniOS Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © DaddyCoding 2020.