0

I seem to be having trouble to navigation to a new segue when there is internet.

In the case for Internet Connectivity I have this:

case ReachableViaWiFi:
view.backgroundColor = .green
internetEnabled()

When the app Runs the View turns green as expected. As you can see there is also the function internetEnabled() which is where the perform segue is:

func internetEnabled() {
    print("1")
    self.performSegue(withIdentifier: "InternetSegue", sender: self)
    print("2")
}

This is the result:

Console Output:

Reachability Flag Status: -R ------- networkStatusForFlags

1

2

As you can see both the prints work but it seems to skip the perform segue.

I have added @IBAction func go to the view controller and when the button is clicked, the segue navigated to the correct ViewController.

Full ViewController.Swift

import UIKit
import SystemConfiguration

var reachability = Reachability()

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(reachabilityStatusChanged(_:)), name: .reachabilityChanged, object: nil)

}

@IBAction func go(_ sender: AnyObject) {
    performSegue(withIdentifier: "InternetSegue", sender: self)
}

func updateInterfaceWithCurrent(networkStatus: NetworkStatus) {
    switch networkStatus {
    case NotReachable:
        view.backgroundColor = .red
        print("No Internet")
    case ReachableViaWiFi:
        view.backgroundColor = .green
        internetEnabled()
    case ReachableViaWWAN:
        view.backgroundColor = .yellow
        print("Reachable Cellular")
    default:
        return
    }

}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //
    updateInterfaceWithCurrent(networkStatus: reachability.currentReachabilityStatus())

}
func reachabilityStatusChanged(_ sender: NSNotification) {
    guard let networkStatus = (sender.object as? Reachability)?.currentReachabilityStatus() else { return }
    updateInterfaceWithCurrent(networkStatus: networkStatus)
}
override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: nil)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func internetEnabled() {
    print("1")
    performSegue(withIdentifier: "InternetSegue", sender: self)
    print("2")
}

}

Question

Can someone help me get this function to perform the segue when there is internet?

Update

When tested on device

Open app. Segue does not perform Turn off WiFi -> View Goes Yelloe Turn on Airplane mode - > View Goes Red Turn off Airplane mode and Turn Wifi back on - Segue works.

JamesG
  • 1,552
  • 8
  • 39
  • 86

1 Answers1

0

This is a little off-topic, but don't use Reachability to detect if the network is online before attempting a request. Make the request and handle errors. Only use Reachability to detect if the network comes back online after being offline - in that case, if your last request resulted in an error, retry.

This post explains it in a little more detail: Reachability responding with wrong status code in iOS 7 iphone 5

Community
  • 1
  • 1
EricS
  • 9,650
  • 2
  • 38
  • 34