Search code examples
iosobjective-ccocoa-touchfacebook-ios-sdkrubymotion

Checking for an active Facebook Session in viewDidAppear


I have a UIViewController subclass called HomeController that is made the rootViewController in my application:applicationDidFinishLaunchingWithOptions.

My intended logical flow works like this:

  1. If the user isn't signed in (determined by whether there's an open, active session using the FB iOS SDK), present a modal view controller asking them to sign in.

  2. If they are signed in, display the HomeController view as normal.

I do a quick check using openSessionWithAllowLoginUI(false) in the app delegate to see whether a cached token exists, as well, which may yield me an active session if the app is reopened after having been closed.

I was placing the session check logic in viewDidAppear and presenting my modal view controller to login. But viewDidLoad logic still gets executed, and the data obviously isn't there for this to function correctly.

What's a better way to be doing this?

Thanks!


Solution

  • One thing you can do is to remove the code using the data fromviewDidLoad. You can have something like:

    def viewDidLoad
      self.view.backgroundColor = UIColor.whiteColor # Or anything you want just to create background screen
    end
    
    def viewDidAppear(animated)
      if alreadyLoggedIn
        createViewAndDoSomethingWithData
      else
        showLogin
      end
    end