互联网重联Swift

时间:2018-04-13 07:29:04

标签: ios swift

如果没有互联网连接,我的应用目前会显示提醒。但是,我希望在检测到互联网连接后自动重新连接,而无需用户重新启动应用程序。

我目前使用的代码是

if Reachability.isConnectedToNetwork() == true {
    print("Internet Connection Available!")
} else {
    let alertController = UIAlertController(title: "Alert",
                                            message: "Internet Connection not Available!",
                                            preferredStyle: UIAlertControllerStyle.alert)
    alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
    self.present(alertController, animated: true, completion: nil)
}
谁能提出一些建议呢?如果我的问题不清楚,请告诉我。大家好!

4 个答案:

答案 0 :(得分:1)

它对我有用

func myconn(){
     if Reachability.isConnectedToNetwork() == true
        {
            print("Internet Connection Available!")
        }
        else
        {
            let alertController = UIAlertController(title: "Alert", message:
                "Internet Connection not Available!", preferredStyle: UIAlertControllerStyle.alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))

            self.present(alertController, animated: true, completion: nil)
              DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: {
               self.myconn()
              })
        }
    }

答案 1 :(得分:1)

您应该在Reachability方法中添加applicationDidFinishLaunching的观察者,如下所示,

NotificationCenter.default.addObserver(self, selector: #selector(networkStatusChanged(_:)), name: NSNotification.Name(rawValue: ReachabilityStatusChangedNotification), object: nil)
Reach().monitorReachabilityChanges()

同样实施此方法,

@objc func networkStatusChanged(_ notification: Notification) {
    let userInfo = (notification as NSNotification).userInfo
    if let status = userInfo?["Status"] as? String, status == "Offline" {
        //Display alert view
    } else if let status = userInfo?["Status"] as? String, status != "Unknown" {
        //Internet connection is active
    }
}

当有活跃的互联网连接时,上述功能会自动触发呼叫。

答案 2 :(得分:0)

您需要添加addObserver的{​​{1}}。

ReachabilityChangedNotification

每当更改网络时,它都会通知您。

答案 3 :(得分:0)

您还可以使用RxSwift观察可达性通知,并在获得新连接状态时处理更改。

像这样:

    var isOnline: Bool {
       guard let reachability = reachability else { return false }
       return reachability.currentReachabilityStatus.isOnline
    }

    func connectionStatus() -> Observable<ConnectionStatus> {
       return notificationCenter
          .rx
          .notification(ReachabilityChangedNotification)
          .observeOn(observeScheduler)
          .flatMap { notification -> Observable<ConnectionStatus> in
              guard let reachability = notification.object as? Reachability else {
                return .empty()
              }

              return .just(ConnectionStatus(isOnline: reachability.isReachable))
          }
          .startWith(ConnectionStatus(isOnline: isOnline))
          .distinctUntilChanged()
}

通过这种方式,您可以观察连接中的任何更改,并可以根据需要对其做出反应。

您只需订阅Observable<ConnectionStatus>,然后您就可以决定是否希望用户触发新的重新连接流程,或者在显示之前重试几次。

相关问题