AppDelegate

2022-11-06  本文已影响0人  弓小员

importUIKit

importSVProgressHUD

importAlamofire

importIQKeyboardManagerSwift

importMJRefresh

@main

class AppDelegate: UIResponder, UIApplicationDelegate {

    varwindow:UIWindow?

    //首次安装App,会弹出是否允许访问网络的弹框。弹框时候是无网络的。等用户点了确认要监听网络恢复

    private var reachabilityManager = Alamofire.NetworkReachabilityManager()

    //上一次的网络状态

    private var lastReachabilityState: NetworkReachabilityManager.NetworkReachabilityStatus = NetworkReachabilityManager.NetworkReachabilityStatus.notReachable

    funcapplication(_application:UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey:Any]?) ->Bool{

        // Override point for customization after application launch.

        window=UIWindow.init()

        lettabbar =TabBarController.init()

        window?.rootViewController= tabbar

        window?.backgroundColor = .white

        window?.makeKeyAndVisible()

        SVProgressHUD.setMinimumDismissTimeInterval(2)

        SVProgressHUD.setMaximumDismissTimeInterval(4)

        SVProgressHUD.setDefaultStyle(.custom)

        SVProgressHUD.setDefaultAnimationType(.native)

        // 键盘不遮挡文本框

        let manager = IQKeyboardManager.shared;

        manager.enable=true;

        manager.shouldResignOnTouchOutside = true;

        manager.shouldToolbarUsesTextFieldTintColor = true;

        manager.enableAutoToolbar=false;

        manager.keyboardDistanceFromTextField = 0;

        startNetworkListening()

        return true

    }

    //首次安装App,会弹出是否允许访问网络的弹框。用户点击确认后,进入这里,重新请求数据

    private func startNetworkListening() {

        guardletreachabilityManager =self.reachabilityManagerelse{return}

        ifreachabilityManager.isReachable{

            //本来就是可以连上网的,后面就不需要监听了

            return

        }

        reachabilityManager.listener= { [weakself] statusin

            guardlet`self` =selfelse{

                return

            }

            switchstatus {

            case.notReachable:

                break

            case.unknown:

                break

            case .reachable(.ethernetOrWiFi), .reachable(.wwan):

                if self.lastReachabilityState == .notReachable || self.lastReachabilityState == .unknown {

                    //之前是网络不可达

                    lettabbar =TabBarController.init()

                    self.window?.rootViewController= tabbar

                }

            }

            self.lastReachabilityState= status

        }

    }

}

上一篇下一篇

猜你喜欢

热点阅读