AppDelegate.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // AppDelegate.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/6.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import IQKeyboardManagerSwift
  10. import PKHUD
  11. @UIApplicationMain
  12. class AppDelegate: UIResponder, UIApplicationDelegate {
  13. var window : UIWindow?
  14. var internetReachability : Reachability?
  15. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  16. // Override point for customization after application launch.
  17. if #available(iOS 13.0, *) {
  18. } else {
  19. //设置键盘
  20. IQKeyboardManager.shared.enable = true
  21. UINavigationBar.appearance().barStyle = .default
  22. //创建window
  23. self.window = UIWindow(frame: UIScreen.main.bounds)
  24. window?.backgroundColor = UIColor.white
  25. //创建根控制器
  26. let vc = IHLoginVCtr()
  27. window?.rootViewController = vc
  28. //显示界面
  29. window?.makeKeyAndVisible()
  30. }
  31. //添加一个系统通知
  32. NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged), name: NSNotification.Name.reachabilityChanged, object: nil)
  33. internetReachability = Reachability.forInternetConnection()
  34. internetReachability?.startNotifier()
  35. updateInterfaceWithReachability(reachability: internetReachability!)
  36. return true
  37. }
  38. //实时检查网络的状态 - 如果从storyboard里启动了,登录时检测不到网络
  39. @objc func reachabilityChanged(noti : Notification){
  40. let curReach = noti.object as! Reachability
  41. updateInterfaceWithReachability(reachability: curReach)
  42. }
  43. private func updateInterfaceWithReachability(reachability : Reachability){
  44. let netStatus = reachability.currentReachabilityStatus()
  45. let keywindow = UIApplication.shared.keyWindow
  46. log.debug("当前的网络状态\(netStatus)--\(keywindow)")
  47. guard let _ = keywindow else {
  48. return
  49. }
  50. switch netStatus {
  51. case NotReachable:
  52. HUD.flash(.label("当前网络状态不可用"), delay: 1)
  53. // case ReachableViaWiFi:
  54. // HUD.flash(.label("当前网络状态是wifi"), delay: 0.6)
  55. // case ReachableViaWWAN:
  56. // HUD.flash(.label("当前网络状态是蜂窝网"), delay: 1)
  57. default:
  58. break
  59. }
  60. }
  61. @available(iOS 13.0, *)
  62. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  63. // Called when a new scene session is being created.
  64. // Use this method to select a configuration to create the new scene with.
  65. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  66. }
  67. @available(iOS 13.0, *)
  68. func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  69. // Called when the user discards a scene session.
  70. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  71. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  72. }
  73. }