-3

I am checking if there is or not internet connection before calling any service method. If there's no internet, I am showing an alert and if I click ok button I want to go to root view controller.

Here's my code:

if TLReachability.isConnectedToNetwork() == true{
    // do here 
} 
else {
    let navigationController = UIApplication.sharedApplication().keyWindow?.rootViewController as! UINavigationController
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let changeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("TLLoginViewController") as! TLLoginViewController
    navigationController.presentViewController(changeViewController, animated: true, completion: nil)
}
redent84
  • 18,901
  • 4
  • 62
  • 85
Ravi
  • 104
  • 2
  • 14

2 Answers2

0
let alert:UIAlertController = UIAlertController.init(title: "ALERT", message: "NO INTERNET", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: { action -> Void in
    let rootViewController = mainStoryboard.instantiateViewControllerWithIdentifier("RootViewController") as! RootViewController
    navigationController.presentViewController(changeViewController, animated: true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
redent84
  • 18,901
  • 4
  • 62
  • 85
oremag14jf
  • 326
  • 3
  • 9
0

Try calling your code on the main thread:

dispatch_async(dispatch_get_main_queue(), {
  let navigationController = UIApplication.sharedApplication().keyWindow?.rootViewController as! UINavigationController 
  navigationController.popToRootViewControllerAnimated(true)
})
koen
  • 5,383
  • 7
  • 50
  • 89