【问题标题】:How do I create a loop checking app connectivity如何创建循环检查应用程序连接
【发布时间】:2018-07-22 03:09:56
【问题描述】:

我正在尝试在我的应用上创建一个循环来检查连接。因此,当应用程序第一次加载时,用户必须连接到互联网,否则他将无法继续使用该应用程序。

但是,我正在尝试创建一个功能,当用户没有互联网时,UIAlerView 会在屏幕上持续存在,直到找到互联网连接,然后它应该被关闭并使用另一种方法。对我来说最好的方法是什么?

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

【问题讨论】:

标签: ios swift xcode loops


【解决方案1】:

你可以使用这个答案

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

在您的应用程序的 appDelegate 级别中。

我认为创建用于检查互联网的循环,会以某种方式迫使处理器内存泄漏,所以我建议不要使用一种循环来检查互联网。

我希望这对你有用。

【讨论】:

  • 您应该为this question创建一个答案,而不是简单地链接到现有答案。或者,在引用现有答案的问题中添加评论。 Timer 可能是定期检查可达性的好方法。循环没有理由导致内存泄漏
猜你喜欢
  • 2017-05-16
  • 2022-07-11
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多