【发布时间】: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)
}
【问题讨论】:
-
创建循环不是一个好主意。我建议为此使用 NSNotification。可能这会有所帮助stackoverflow.com/questions/27310465/…
-
你好老兄,你能发帖回答吗?