gpt4 book ai didi

ios - 如何在没有网络连接的情况下持续出现警报?

转载 作者:行者123 更新时间:2023-11-30 11:18:44 24 4
gpt4 key购买 nike

我正在开发一个需要互联网连接才能正常运行的 iOS 应用程序,因此我使用可达性框架来获取连接状态。现在,只要没有互联网连接,我就会发出警报,并显示“再试一次”按钮,我希望警报在没有互联网的情况下持续出现。你能帮我吗?谢谢!

顺便说一句,如果你认为我应该改变什么,请告诉我!干杯!

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

最佳答案

与其使用警报,不如使用隐藏的标签,并且在没有互联网时不隐藏标签。连接恢复后再次隐藏

关于ios - 如何在没有网络连接的情况下持续出现警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51521580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com