AppDelegate的应用开始设置错误

时间:2019-04-05 08:19:28

标签: swift appdelegate

我添加了appdelegate didfinishlaunchingwithoptions标签栏菜单。添加此菜单后打开应用程序时,它总是从MainTableView开始。即使我退出了应用程序,当应在MainTableView中打开应用程序时,也会打开LoginViewController页面。换句话说,我希望在用户退出应用程序后打开应用程序时返回登录页面。添加tabbar菜单后,我遇到了这样的问题。

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate, UITabBarControllerDelegate {

var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
            // For iOS 10 data message (sent via FCM
            Messaging.messaging().delegate = self
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()
        FirebaseApp.configure()

        loadDefaults()



        let tabBarController = ESTabBarController()
        tabBarController.delegate = self
        //  tabBarController.navigationController?.setNavigationBarHidden(true, animated: false);
        tabBarController.tabBar.shadowImage = UIImage(named: "transparent")
        tabBarController.tabBar.backgroundImage = UIImage(named: "background_dark")
       // tabBarController.tabBar.backgroundColor = UIColor.flatGray
        tabBarController.shouldHijackHandler = {
            tabbarController, viewController, index in
            if index == 2 {
                return true
            }
            return false
        }
        tabBarController.didHijackHandler = {
            [weak tabBarController] tabbarController, viewController, index in

            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                let alertController = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
                let takePhotoAction = UIAlertAction(title: "Take a photo", style: .default, handler: nil)
                alertController.addAction(takePhotoAction)
                let selectFromAlbumAction = UIAlertAction(title: "Select from album", style: .default, handler: nil)
                alertController.addAction(selectFromAlbumAction)
                let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
                alertController.addAction(cancelAction)
                tabBarController?.present(alertController, animated: true, completion: nil)
            }
        }

        let v1 = MainTableViewController()
        let v2 = NewMainTableViewController()
        let v3 = MainTableViewController()
        let v4 = ContactViewController()

        DispatchQueue.main.asyncAfter(deadline: .now() + 2 ) {
            v2.tabBarItem.badgeValue = "1"
        }

        v1.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(), title: "Anasayfa", image: UIImage(named: "homepage"), selectedImage: UIImage(named: "socket-4"))
        v2.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(), title: "Yeni Cihaz", image: UIImage(named: "socketmenu"), selectedImage: UIImage(named: "energy"))
        v3.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(), title: nil, image: UIImage(named: "homepage"), selectedImage: UIImage(named: "homepage"))
        v4.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(), title: "İletişim", image: UIImage(named: "homepage"), selectedImage: UIImage(named: "contactmenu-1"))


        tabBarController.viewControllers = [v1, v2, v3, v4]

        let navigationControllerNew = ExampleNavigationController.init(rootViewController: tabBarController)


        let leftbutton = UIButton(type: .system)
        leftbutton.setImage(UIImage(named:"settings")?.withRenderingMode(.alwaysOriginal), for: .normal)
        leftbutton.frame = CGRect(x:0, y:0, width:20, height:20)
        leftbutton.addTarget(self, action: #selector(settingButton(sender:)), for: .touchUpInside)
        tabBarController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftbutton)

        let rightbutton = UIButton(type: .system)
        rightbutton.setImage(UIImage(named:"logout")?.withRenderingMode(.alwaysOriginal), for: .normal)
        rightbutton.frame = CGRect(x:0, y:0, width:20, height:20)
        rightbutton.addTarget(self, action: #selector(handleLogout), for: .touchUpInside)
        tabBarController.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightbutton)


        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        imageView.contentMode = .scaleAspectFit

        // 4
        let image = UIImage(named: "Icon-App-40x40")
        imageView.image = image

        // 5
        tabBarController.navigationItem.titleView = imageView

        tabBarController.navigationController?.navigationBar.backgroundColor = UIColor.flatSand
        tabBarController.navigationController?.navigationBar.isTranslucent = true

        window = UIWindow(frame: UIScreen.main.bounds)

    //let navigationController = UINavigationController(rootViewController: MainTableViewController())
     // window?.rootViewController = navigationController

        window?.rootViewController = navigationControllerNew

        window?.makeKeyAndVisible()
        return true
    }
    @objc func handleLogout(){

        do {
            try Auth.auth().signOut()
            let mainTableVC = LoginViewController()

            let navController = UINavigationController(rootViewController: mainTableVC)

            self.window?.rootViewController?.present(navController, animated: true, completion: {

                //
            })

        } catch let signOutError as NSError {
            print ("Giriş Yapılamadı: %@", signOutError)
        }
    }
 private func loadDefaults() {
        let userDefaults = UserDefaults.standard

        let pathStr = Bundle.main.bundlePath
        let settingsBundlePath = (pathStr as NSString).appendingPathComponent("Settings.bundle")
        let finalPath = (settingsBundlePath as NSString).appendingPathComponent("Root.plist")
        let settingsDict = NSDictionary(contentsOfFile: finalPath)
        guard let prefSpecifierArray = settingsDict?.object(forKey: "PreferenceSpecifiers") as? [[String: Any]] else {
            return
        }

        var defaults = [String: Any]()

        for prefItem in prefSpecifierArray {
            guard let key = prefItem["Key"] as? String else {
                continue
            }
            defaults[key] = prefItem["DefaultValue"]
        }
        userDefaults.register(defaults: defaults)

    }
 var backgroundUpdateTask: UIBackgroundTaskIdentifier!


    func applicationWillResignActive(_ application: UIApplication) {
        self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
            self.endBackgroundUpdateTask()
        })
    }

    func endBackgroundUpdateTask() {
        UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
        self.backgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
    }

    func applicationDidEnterBackground(_ application: UIApplication) {

    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        self.endBackgroundUpdateTask()

    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        SettingsBundleHelper.checkAndExecuteSettings()
        SettingsBundleHelper.setVersionAndBuildNumber()
    }

0 个答案:

没有答案