simplyi / user-registration

23 stars 14 forks source link

AppDelegate, if accestoken is not nil problem #3

Open robert1993 opened 4 years ago

robert1993 commented 4 years ago

Hi Sergey,

When I try to let the application check if the accestoken is not nil and then go to the 'protected' view controller nothing is happening.

This is my Appdelegate:

` // // AppDelegate.swift // Tempi // // Created by Robert Sprenkels on 29/03/2020. // Copyright © 2020 Robert Sprenkels. All rights reserved. //

import UIKit import SwiftKeychainWrapper

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")

           if accessToken != nil {
             let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
             let homePage = mainStoryboard.instantiateViewController(identifier: "UITabBarController") as! UITabBarController
         //UITabBarController
             self.window?.rootViewController = homePage

        //   self.performSegue(withIdentifier: "ToSignIn", sender: "Any.")

           }

    return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

}

`

I Gues something has changed over the years, the way that swift handles this action but I can't find what it is. Can you help me out?

The accessToken is working just fine and is not nil.

interested-developer commented 4 years ago

If you set a breakpoint in the UITabBarController's viewWillAppear() method, will it trigger?

interested-developer commented 4 years ago

Also, does the UITabBarController has an identifier "UITabBarController" set?

robert1993 commented 4 years ago

Sorry for my late respons, I was to busy with other things last weeks.

No the method does not trigger although when I print the accestoken it does show up properly.

I've changed the code to

let homePage = mainStoryboard.instantiateViewController(identifier: "LiveViewController") as! LiveViewController

Since I Gues I can't use a tabbarcontroller to point to, but I need to use the first tab. I'm 100% sure the identifier was set correctly

robert1993 commented 4 years ago

Solved it by adding this code to the signIn page.

` override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated)

    // Do any additional setup after loading the view.

    let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")
    print(accessToken!)
           if accessToken != nil {

           self.performSegue(withIdentifier: "ToSignIn", sender: "Any.")

    }
}`