I am logging in using facebook SDK and everything is properly setup. When I authorize my app for first time, the segue gets performed to my next view controller. But when I logout and try to login again, the segue gives an error
fatal error: unexpectedly found nil while unwrapping an Optional value
This is my code in my appDelegate file
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool{
let result = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
if result {
let token = FBSDKAccessToken.currentAccessToken()
let fieldsMapping = [
"id" : "facebookId",
"name" : "name",
"email": "email"
]
backendless.userService.loginWithFacebookSDK(
token,
fieldsMapping: fieldsMapping,
response: { (user: BackendlessUser!) -> Void in
print("user: \(user)")
self.backendless.userService.setStayLoggedIn(true)
loadUser()
// Access the storyboard and fetch an instance of the view controller
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController: MainScreenViewController = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as! MainScreenViewController
let rootViewController = self.window!.rootViewController! as UIViewController
print(rootViewController)
rootViewController.presentViewController(viewController, animated: true, completion: nil)
},
error: { (fault: Fault!) -> Void in
print("Server reported an error: \(fault)")
})
}
return result
}
the error occurs at rootViewController.presentViewController(viewController, animated: true, completion: nil)
I have tried performing segue in my viewController in viewDidAppear(), and also tried performSegueWithIdentifier() but the error persists. If I kill my application and try again, the segue works fine. Please suggest me where is the problem. Thanks in advance.