[ios] How to check for an active Internet connection on iOS or macOS?

Please try this, it will help you (Swift 4)

1) Install Reachability via CocoaPods or Carthage: Reachability

2) Import Reachability and Use in Network Class

import Reachability
class Network {

   private let internetReachability : Reachability?
   var isReachable : Bool = false

   init() {

       self.internetReachability = Reachability.init()
       do{
           try self.internetReachability?.startNotifier()
           NotificationCenter.default.addObserver(self, selector: #selector(self.handleNetworkChange), name: .reachabilityChanged, object: internetReachability)
       }
       catch {
        print("could not start reachability notifier")
       }
   }

   @objc private func handleNetworkChange(notify: Notification) {

       let reachability = notify.object as! Reachability
       if reachability.connection != .none {
           self.isReachable = true
       }
       else {
           self.isReachable = false
       }
       print("Internet Connected : \(self.isReachable)") //Print Status of Network Connection
   }
}

3) Use like Below where You need.

var networkOBJ = Network()
// Use "networkOBJ.isReachable" for Network Status
print(networkOBJ.isReachable) 

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to cocoa

How to update a single pod without touching other dependencies How to initialise a string from NSData in Swift Input from the keyboard in command line application Get current NSDate in timestamp format Xcode build failure "Undefined symbols for architecture x86_64" Cocoa Autolayout: content hugging vs content compression resistance priority setValue:forUndefinedKey: this class is not key value coding-compliant for the key iOS - Build fails with CocoaPods cannot find header files Get Current date & time with [NSDate date] Remove all whitespaces from NSString

Examples related to cocoa-touch

Include of non-modular header inside framework module Move textfield when keyboard appears swift Create space at the beginning of a UITextField Navigation bar with UIImage for title Generate a UUID on iOS from Swift How do I write a custom init for a UIView subclass in Swift? creating custom tableview cells in swift How would I create a UIAlertView in Swift? Get current NSDate in timestamp format How do you add an in-app purchase to an iOS application?

Examples related to reachability

Check for internet connection with Swift Easiest way to detect Internet connection on iOS? How to check for an active Internet connection on iOS or macOS?