I used Reachability Class from apple to check internet connection. I'll share to codes in the below. It's working fine if i open the app with online. But if i try to open app offline Reachability function is not triggered. Can anyone help me to solve this issue. Thank you
Here is the code which are locate in AppDelegate.swift
var reachability: Reachability?
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "checkForReachability:", name:kReachabilityChangedNotification, object: nil)
reachability = Reachability.reachabilityForInternetConnection();
reachability?.startNotifier();
return true
}
func checkForReachability(notification: NSNotification) {
let title_alert = NSLocalizedString("Connection Problem", comment: "Connection Problem")
let remoteHostStatus = self.reachability!.currentReachabilityStatus()
if (remoteHostStatus == NotReachable) {
let myAlert = UIAlertController(title:title_alert, message:NSLocalizedString("Please Check Your Connection", comment: "Connection Problem"), preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title:NSLocalizedString("Try Again",comment:"Connection Problem"), style:UIAlertActionStyle.Default) {
action in
self.checkForReachability(notification)
}
myAlert.addAction(okAction)
self.window?.rootViewController?.presentViewController(myAlert, animated:true, completion:nil)
}
}