1
votes

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.

1
Which viewContoller is your rootViewController ?Umair Afzal
The problem might be in your MainScreenViewController.apetrov
there is rootViewController, i get my ViewController where the facebook login button is as my rootVIewController when i print it.Arunjot Singh
@AleksandarPetrov what issue could be in my MainScreenViewController?Arunjot Singh
For an example using not linked outlet.apetrov

1 Answers

1
votes

I think there is no rootViewController so you need to set it like below

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

 var rootView: MyRootViewController = MyRootViewController()

 if let window = self.window{
        window.rootViewController = rootView
 }
 return true
}

and then use it afterwords.