-1

im trying to set the initial view controller if a user has a token in his keychain.

when I try to set it in the appDelegate like this

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")
    if accessToken != nil
    {
        // Take user to a home page
        let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let homePage = mainStoryboard.instantiateViewController(withIdentifier: "home") as! ViewLoader
        self.window.rootViewController = homePage
    }
    return true
}

but I get som errors saying "Value of type 'AppDelegate' has no member 'window'"

it tried to clean the project but without luck

sebbelebbe
  • 74
  • 1
  • 9

2 Answers2

0

The reason of your error is you do not declare a variable window.

Adding following code in your AppDelegate class is a solution for error:

class AppDelegate: UIResponder, UIApplicationDelegate {

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

    let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let homePage = mainStoryboard.instantiateViewController(withIdentifier: "home")
    self.window?.rootViewController = homePage
    self.window?.makeKeyAndVisible()

    return true
  }
  // ..... more code ...
}
Kenma
  • 33
  • 2
rs7
  • 1,618
  • 1
  • 8
  • 16
0

Try this:

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

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homePage = mainStoryboard.instantiateViewController(withIdentifier: "home") as! ViewLoader
window?.rootViewController = homePage

window?.makeKeyAndVisible()

Init window first and add the function makeKeyAndVisible.

Lynx
  • 381
  • 1
  • 13