0

I'm trying to create a loop on my app to check Connectivity. So, when the app loads for the first time, the user must be connected to the internet, otherwise he cannot proceed on using the app.

However, I'm trying to create a function that while the user has no internet, the UIAlerView persists on the screen until find an internet connection, then it should be dismissed and another method be lunched. What would be the best way for me to do that?

   let alert = UIAlertController(title: "Primeiro Uso", message: "Voce precisa estar conectado na internet", preferredStyle: UIAlertControllerStyle.alert)

                        // add the actions (buttons)
                        alert.addAction(UIAlertAction(title: "Estou Conectado", style: .default, handler: { action in

                            if Connectivity.isConnectedToInternet != true {

                             //How do I create a loop here until find a connection?
                             //I'd like to persist the UIAlerView until find a connection


                            }
                            //Once the user is connected, this code below should be
                            //lunched
                            getApiData { (cerveja) in
                                arrCerveja = cerveja
                                //Backup
                                do{
                                    let data = try JSONEncoder().encode(arrCerveja)

                                    UserDefaults.standard.set(data, forKey: "backupSaved")
                                    //
                                    self.tableView.reloadData()
                                }catch{print(error)
                                }

                            }}))

                        alert.addAction(UIAlertAction(title: "Cancelar", style: UIAlertActionStyle.cancel, handler: nil))

                        // show the alert
                        self.present(alert, animated: true, completion: nil)
                    }
TituJow
  • 119
  • 1
  • 9
  • Creating a loop is not a good idea. I would suggest using NSNotification for this. May be this will help https://stackoverflow.com/questions/27310465/detecting-network-connectivity-changes-using-reachability-nsnotification-and-ne – Dhivysh Jul 22 '18 at 09:53
  • Hello dude, can you post as an answer? – TituJow Jul 22 '18 at 15:25

1 Answers1

-1

you can use this answer

https://stackoverflow.com/a/3597085/2621857

in the appDelegate level of you application.

i think creating loop for checking internet, somehow will pressure the processor to memory leak so i suggest not using a kind of loop to check the internet.

i hope this will work for you .

  • Rather than simply linking to an existing answer you should create an answer for *this question*. Alternatively, add a comment to the question referencing the existing answer. A `Timer` could be a good approach for periodically checking reachability. There is no reason that a loop should cause a memory leak – Paulw11 Jul 22 '18 at 07:51