Swift 3: Internet access check (re-creation) ViewDidAppear not

Ok, so I'm pretty new to Swift, and I'm a little confused about what I'm trying to do, or if I go in the wrong direction. ( https://github.com/ashleymills/Reachability.swift )

Here is my viewDidLoad method:

 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. checkConnection() } 

Then I have a function with code from the Reachability GitHub project:

 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") } } 

As you can see when there is no Internet, this is the code:

 self.dim(direction: .In, alpha: self.dimLevel, speed: self.dimSpeed) self.performSegue(withIdentifier: "mainToAlertNoInternet", sender: self) 

The Dim is taken from ( http://www.totem.training/swift-ios-tips-tricks-tutorials-blog/ux-chops-dim-the-lights ) mainToAlertNoInternet loads the next view on top of the current one with transparency, so itโ€™s alert style .

So, the second segue has a view and a button on it. Nothing exciting is what loads when there is no Internet:

enter image description here

This button to try again is connected with the Segue output and launches this function in the First View controller:

 @IBAction func unwindFromSecondary(segue: UIStoryboardSegue) { dim(direction: .Out, speed: dimSpeed) checkInternetConnection() } 

I added mainToAlertNoInternet to the function mainToAlertNoInternet that when I click it again, it will return to the first session and run the test again. However, when I click the Retry button, I get this error:

A warning. Try to imagine whose view is not in the hierarchy of windows!

I hope I explained in sufficient detail what I created. Now to the questions:

1) How can I fix this error? 2) Am I doing it right or is there a better way?

This is what I want:

I want to check the Internet connection at the time of downloading the application. If there is no connection, I want to display the shogu, as I did. If the user clicks the Try Amplification button, I want him to return to the first controller and run the test again, and if the connection still does not appear, as it was done earlier. I would like this to be a repeating process until there is Internet access.

Appreciate all your help. thanks in advance

Edit:

I added a function call in the ViewDidAppear method as follows:

 override func viewDidAppear(_ animated: Bool) { checkInternetConnection() } 

However, it does not start. Also, the DIM in the unwindFromSecondary function unwindFromSecondary not called when I do this.

Edit 2:

Just added this line to my viewDidAppear :

 print("Appeared") 

It is called first, but not again.

Question:

How to get a function to run after everything has loaded again after unindSegue?

Any thoughts?

Update 3

Ok, so I reviewed the answers below:

@MarkP Answer works fine. Thank you for that. However, the answer from @iSee made me think, maybe I should figure it out differently.

I added Bounty to this post for a detailed answer that can show me and explain how to achieve the following:

In my application. I need to constantly ensure that the Internet exists (possibly a timer) in any view that is loading. I would like, as in the current case, if there is no Internet, it will pop up in the ViewController using this session:

 performSegue(withIdentifier: "mainToAlertNoInernet", sender: self) 

It looks like the app delegate will be the place, but I'm not sure how to do this.

I am new to iOS Development and will therefore be grateful for some explanation and teaching.

Thank you for your time. This is very valuable.

+8
ios reachability swift swift3
source share
3 answers

As @iSee pointed to the links. This is because the view is not added to the view hierarchy, so you cannot go to it. this requires self.dismissViewControllerAnimated :

 @IBAction func unwindFromSecondary(segue: UIStoryboardSegue) { dim(direction: .Out, speed: dimSpeed) self.dismiss(animated: true) { self.checkInternetConnection() } } 
+2
source share

I assume you are new to iOS development in general.

  • A warning has nothing to do with your internet connection code.

  • The warning you receive is not an error. This is just a warning.

  • The reason you get this is well explained in this link and this .
  • To get rid of this warning, you should not call the performSegue function from viewDidLoad (see the link above for more information.
  • To perform a network check, it is recommended to use AppDelegate (gives you better control over the flow of the application).

All the best :)

EDIT: see this link here for more info on this. I could easily retype it here, but since it already answered, you can refer to the link above about why this is happening and how to avoid it.

+3
source share

I use this in my application โ†’

  func reachablityCode() { AFNetworkReachabilityManager.sharedManager() AFNetworkReachabilityManager.sharedManager().startMonitoring() AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock({(status) in let defaults = NSUserDefaults.standardUserDefaults() if status == .NotReachable { defaults.setBool(false, forKey:REACHABLE_KEY) } else { defaults.setBool(false, forKey: REACHABLE_KEY) } defaults.synchronize() }) } 

And then this is in the base file โ†’

  func isReachable() -> Bool { return NSUserDefaults.standardUserDefaults().boolForKey(REACHABLE_KEY) } 
0
source share

All Articles