0

I am working on an iOS app that requires internet connection to run properly so i'm using the Reachability framework so that I can obtain the connection state. Right now whenever there is no internet connection I have and alert with the button saying "Try Again" showing up once and what I want it's the alert to be continuously appear while there's no internet. Can you help me please? Thanks!

By the way, if you think there is something I should change go ahead and tell me! Cheers!

class ViewController: UIViewController {

let reachability = Reachability()!

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

override func viewDidAppear(_ animated: Bool) {

    reachability.whenReachable = { reachability in
        if reachability.connection == .wifi {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    }
    reachability.whenUnreachable = { _ in
        print("Not reachable")
    }

    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }

    NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
    do{
        try reachability.startNotifier()
    }catch{
        print("could not start reachability notifier")
    }
}

@objc func reachabilityChanged(note: Notification) {

    let reachability = note.object as! Reachability

    switch reachability.connection {
    case .wifi:
        print("Reachable via WiFi")
    case .cellular:
        print("Reachable via Cellular")
    case .none:
        print("Network not reachable")
        createAlert(title: "No Internet Connection", message: "Internet Connection is required fot this application to run properly")
    }
}

func createAlert(title:String, message:String)
{
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

    alert.addAction(UIAlertAction(title: "Try Again", style: UIAlertActionStyle.default, handler: { (action) in alert.dismiss(animated: true, completion: nil) } ) )

    self.present(alert, animated: true, completion: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
JoaoPimentao
  • 41
  • 1
  • 9
  • I think a better practice would be to show the alert with "try again" once something actually fails because of the lack of internet connection - you can handle callbacks from the networking frameworks. – user3581248 Jul 25 '18 at 14:48
  • I'm sorry but can you be a little more explicit? I know, from what I have read, that is better practice to only alert when it can't get to the REST api but for this particular situation I want it to automatically warn the user that he has no connection . And I want that alert to keep appearing while it doesn't have connection. – JoaoPimentao Jul 25 '18 at 15:15
  • Well whatever it is your app is doing, your network requests will fail if there's no connection. So that's the place to handle the lack of connection errors. – user3581248 Jul 25 '18 at 15:16
  • The thing is that it doesn't fail. It keeps running but the content that's supposed to be shown doesn't appears. – JoaoPimentao Jul 25 '18 at 15:18

1 Answers1

0

Instead of an alert, it might be better to use a label that is hidden, and un-hidden when there is no internet. then hide again once the connection is back

Eric D'Souza
  • 680
  • 5
  • 21
  • Alright! I like that idea! I will get back to you once I tried that! Thank you very much but i have a do have a question. One thing I like about the alert is that the background becomes inaccessible for the user. Is there anyway to "block" the user from the background using a label? – JoaoPimentao Jul 25 '18 at 20:43
  • You can add a screen-sized button that covers the screen. It would also intercept any taps (you don’t need to do anything with the tap, just receive the tap event). You can make the button 50% transparent to give a similar effect as an Alert. On top of the button you can add the label with your message. Both the button and label appear / disappear based on network connectivity – Eric D'Souza Jul 26 '18 at 21:41