Add Reachability.h and Reachability.m files in your swift project.
Create Bridging_Header
#ifndef xxx_Bridging_Header_h
#define xxx_Bridging_Header_h
#import "Reachability.h"
#endif /* xxx_Bridging_Header_h */
AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var isReachable: Reachability!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.isReachable = Reachability.forInternetConnection()
NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged(_:)), name: NSNotification.Name.reachabilityChanged,
object: nil)
self.isReachable!.startNotifier()
return true
}
//Reachbality Notification Response
@objc func reachabilityChanged(_ notification: Notification) {
if self.isReachable.isReachableViaWiFi() || self.isReachable.isReachableViaWWAN() {
print("Service avalaible!!!")
} else {
print("No service avalaible!!!")
}
}
}
Constants.swift
import Foundation
import UIKit
class Constants: NSObject {
struct ReferenecVariables {
static let appDelegate = UIApplication.shared.delegate as! AppDelegate
}
}
Viewcontroller.swift
...
@IBAction func loginButtonClicked() {
if Constants.ReferenecVariables.appDelegate.isReachable.isReachable() {
//network available
}else {
//network not available.
}
}
...
Or Another Answer
Add SystemConfiguration.framework

import UIKit
import SystemConfiguration
....
....
@IBAction func signInButtonTapped(sender:AnyObject){
let status:Bool = self.checkInternet()
if status == false {
//Network not available
}else{
// Network available
}
}
func checkInternet() -> Bool{
//CHECK INTERNET CONNECTION
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
}) else {
return false
}
var flags: SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return false
}
let isReachable = flags.contains(.reachable)
let needsConnection = flags.contains(.connectionRequired)
return isReachable && !(needsConnection)
}