如何通过可达性异步检查Internet连接并显示通知栏? (快速)

问题描述 投票:0回答:1

[我希望我的应用程序能够在我的应用程序断开连接时,通过流行的库可访问性(不是Apple的本机)通过顶部的红色通知/状态横幅让我知道。

我想要达到的目标的一个例子就是《口袋妖怪GO》中的东西。 https://imgur.com/T29rJKV

我只得到了可以正常工作的代码,但是没有检测到何时重新启用Wi-Fi。

class ViewController: UIViewController {

let reachability = try! Reachability()

override func viewDidLoad() {
    super.viewDidLoad()

    //Does not work when internet connection is offline, then comes back online
    reachability.whenReachable = { _ in
          print("Reachable")
      }

      reachability.whenUnreachable = { _ in
          print("Not reachable")
      }

    //Don't know what notifier is, seems to fire off above closures
    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }

}

我如何使用可达性库来异步循环/不断检查互联网连接是否存在问题?如何在选项卡控制器上创建类似的状态栏,以便每当互联网出现问题时,应用程序流中的所有视图都会创建此红色状态栏?

ios swift connection reachability
1个回答
0
投票

您可以在调用API之前进行检查。我认为《 Pokemon Go》也采取了同样的策略。只是因为他们经常调用API。

© www.soinside.com 2019 - 2024. All rights reserved.