title: Swift 3: Checking Internet (Reocurring) viewDidAppear not
body: <p>Ok, So I am rather new to Swift and I am a little confused about what I am trying to do, or if I am going in the wrong direction. (<a href="https://github.com/ashleymills/Reachability.swift" rel="nofollow noreferrer">https://github.com/ashleymills/Reachability.swift</a>)</p>
  
  <p>Here is my <code>viewDidLoad</code> method : </p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
  
      checkConnection()
  
  }
  </code></pre>
  
  <p>I then have a function with the code in from the Reachability GitHub Project:</p>
  
  <pre><code>func checkConnection() {
      //declare this property where it won't go out of scope relative to your listener
      let reachability = Reachability()!
  
      reachability.whenReachable = { reachability in
          // this is called on a background thread, but UI updates must
          // be on the main thread, like this:
          DispatchQueue.main.async {
              if reachability.isReachableViaWiFi {
                  print("Reachable via WiFi")
              } else {
                  print("Reachable via Cellular")
              }
          }
      }
      reachability.whenUnreachable = { reachability in
          // this is called on a background thread, but UI updates must
          // be on the main thread, like this:
          DispatchQueue.main.async {
  
              self.dim(direction: .In, alpha: self.dimLevel, speed: self.dimSpeed)
              self.performSegue(withIdentifier: "mainToAlertNoInternet", sender:  self)
          }
      }
  
      do {
          try reachability.startNotifier()
      } catch {
          print("Unable to start notifier")
      }
  }
  </code></pre>
  
  <p>As you can see, when there is no internet, this is the code:</p>
  
  <pre><code>self.dim(direction: .In, alpha: self.dimLevel, speed: self.dimSpeed)
              self.performSegue(withIdentifier: "mainToAlertNoInternet", sender:  self)
  </code></pre>
  
  <p>The Dim is taken from (<a href="http://www.totem.training/swift-ios-tips-tricks-tutorials-blog/ux-chops-dim-the-lights" rel="nofollow noreferrer">http://www.totem.training/swift-ios-tips-tricks-tutorials-blog/ux-chops-dim-the-lights</a>) the <code>mainToAlertNoInternet</code> loads the next view over the current one with transparence so it is an alert style.</p>
  
  <p>So, the second segue has a view and a button on it. Nothing spectacular this is what is loaded when there is no internet:</p>
  
  <p><a href="https://i.stack.imgur.com/x9IXF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x9IXF.png" alt="enter image description here"></a></p>
  
  <p>That try again button is linked to the Exit of the Segue and runs this function in the First View Controller:</p>
  
  <pre><code>@IBAction func unwindFromSecondary(segue: UIStoryboardSegue) {
      dim(direction: .Out, speed: dimSpeed)
      checkInternetConnection()
  }
  </code></pre>
  
  <p>I added in the function <code>mainToAlertNoInternet</code> so that when they click try again, it will go back to the first segue and run the test again. However, When I click try again, I get this error: </p>
  
  <blockquote>
    <p>Warning: Attempt to present  on  whose view is not in the window hierarchy!</p>
  </blockquote>
  
  <p>Hopefully I have explained enough what I have set up. Now to the Questions:</p>
  
  <p>1) How can I fix this error?
  2) Am I doing this the right way or is there a better way?</p>
  
  <p>This is what I want:</p>
  
  <p>I want to check the internet connection the moment the app loads. If there is no connection I want to display the segue like I have been doing. If the user clicks Try gain, I want it to go back to the first controller and run the check again and if still no connection display the segue like it did initially again. I would like this to be a repeating process until there is internet. </p>
  
  <p>Appreciate all your help. Thanks in advance</p>
  
  <h1>Edit:</h1>
  
  <p>I have added the function call in the ViewDidAppear method like so:</p>
  
  <pre><code>override func viewDidAppear(_ animated: Bool) {
      checkInternetConnection()
  }
  </code></pre>
  
  <p>However, it does not run. Also the DIM in the <code>unwindFromSecondary</code> function does not get called when I do this. </p>
  
  <h1>Edit 2:</h1>
  
  <p>Just added this line into my <code>viewDidAppear</code>:</p>
  
  <pre><code>print("Appeared")
  </code></pre>
  
  <p>This gets called initially, but then not again.</p>
  
  <p>Question:</p>
  
  <p>How do I get a function to run once everything has loaded again after the unwindSegue?</p>
  
  <p>Any thoughts?</p>
  
  <h1>Update 3</h1>
  
  <p>Ok, so I have looked at the answers below:</p>
  
  <p><a href="https://stackoverflow.com/a/39563554/5675125">@MarkP</a> Answer works fine. Thank you for that 
  However, the answer from <a href="https://stackoverflow.com/a/39548386/5675125">@iSee</a> has got me thinking maybe I should be going about this a different way. </p>
  
  <p>I have added a Bounty to this post for a detailed answer that can show me and explain how to achieve the following: </p>
  
  <p>In my app. I need to keep making sure that the internet exists (Maybe a Timer) on any view that loads. I would like it so that like the current way, If there is no internet it will pop up the ViewController with this segue: </p>
  
  <pre><code>performSegue(withIdentifier: "mainToAlertNoInernet", sender: self)
  </code></pre>
  
  <p>It appears that the App Delegate would be the place but I am unsure how to achieve this.</p>
  
  <p>I am new to iOS Development and thus would appreciate some explanation and teaching.</p>
  
  <p>Thank you for your time. It is greatly appreciated. </p>