面临可达性问题 IOS Swift

Facing Issue with Reachability IOS Swift

我在该项目的一个演示项目中创建了可达性我有两个视图控制器如果有互联网连接我会显示 OnlineViewController 如果没有互联网连接我会显示 OfflineViewController。请参阅我在 Appdelegate.swift.

中实现的以下代码
 var reachability:Reachability?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        NotificationCenter.default.addObserver(self, selector: #selector(reachabilityStatusChanged(_:)), name: .reachabilityChanged, object: nil)
        reachability = Reachability.forInternetConnection()
        reachability!.startNotifier()
            return true
    }

我的功能是这样的

 @objc func reachabilityStatusChanged(_ sender: NSNotification) {

        var remoteHostStatus = self.reachability!.currentReachabilityStatus()

        if (remoteHostStatus == NotReachable)
        {
            print ("no net")
            let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "OfflineViewController") as! OfflineViewController
            window!.rootViewController = testController
            window!.makeKeyAndVisible()
        }
        else
        {
            let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "OnlineViewController") as! OnlineViewController
            window!.rootViewController = testController
            window!.makeKeyAndVisible()
            print (" wifi")

        }

    }

现在我的问题是我想在我的实时项目中实现同样的事情,在那个项目中我有 3 个故事板和多个视图控制器。那么如果有互联网连接,如何再次显示用户当前屏幕。

在您的项目中以同样的方式使用此功能并将用户导航到特定的视图控制器。

更多细节请参考这篇link。

import UIKit
import Reachability
class CommonUtility: NSObject {

    class func isConnected() -> Bool{
        let reachability:Reachability = Reachability.forInternetConnection()
        let networkStatus = reachability.currentReachabilityStatus().rawValue
        return networkStatus != 0
   }
}

//在下方使用 link 下载它,然后将 Reachability 文件夹拖放到您的项目中

https://github.com/ashleymills/Reachability.swift

pod 'ReachabilitySwift', '~> 4.1'

注意:这里的ReachabilitySwift版本是基于swift版本的,你必须安装

let reachability = Reachability()!

  override func viewDidLoad()
  {
    super.viewDidLoad()
  }


 if (reachability.isReachable)
   {
         // your code
   }
 else
   {
       // your code
   }

我使用以下代码解决了我的问题。不知道我的回答对还是错

@objc func reachabilityStatusChanged(_ sender: NSNotification) {

        var remoteHostStatus = self.reachability!.currentReachabilityStatus()

        if (remoteHostStatus == NotReachable)
        {
            print ("no net")
            let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "OfflineViewController") as! OfflineViewController
            window!.rootViewController = testController
            window!.makeKeyAndVisible()
        }
        else
        {
          UserTypeValidateCall()

        }

    }.

//MARK:- Validating User Type
func  UserTypeValidateCall()
{
    let Usertype  =  UserDefaults.standard.string(forKey: "Usertype")
    print("Usertype-----",Usertype)

    if(( Usertype) != nil)
    {
        if (Usertype?.isEqual("Parent"))!
        {
            ParentDashBoardEnter()
        }
        else  if (Usertype?.isEqual("Employee"))!
        {
            TeacherDashBoardEnter()
        }
        else if (Usertype?.isEqual("Admin"))!
        {
            AdminDashBoardEnter()
        }
    }
    else
    {
        LoginScreenEnter()
    }
}