0

I have a tabbed Swift app (and new to app development) and I have the FB Core and Login kit.

If the user is logged in (FBSDKAccessToken.currentAccessToken() is not nil) then I'd like to display the view as normal. If they're not I'd like to display a login view with a button to authorize the app.

I know how to make the button, my question is what's the best place/way to interrupt the app and redirect to the login view. This should probably also do the check and redirect when coming back on to the app.

I saw this answer showing how you can interrupt and load a different view in func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?).

I suppose I should also check in func applicationDidBecomeActive(application: UIApplication)?

Facebook says to use the SDK as follows:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // showLoginIfUnauthed() ??
    return FBSDKApplicationDelegate.sharedInstance()
        .application(application, didFinishLaunchingWithOptions: launchOptions)
}

// Overload when coming back from authenticating
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance()
        .application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

func applicationDidBecomeActive(application: UIApplication) {
    // showLoginIfUnauthed() ??
    FBSDKAppEvents.activateApp()
}

I was thinking of calling a method in these three functions, is this the wrong way of going about this?:

func showLoginIfUnauthed() {
    if !FBSDKAccessToken.currentAccessToken() {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        var loginController: LoginViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LoginController") as! LoginViewController

        self.window?.rootViewController = loginController

        self.window?.makeKeyAndVisible()
    }
}

Thanks.

main storyboard

Community
  • 1
  • 1
Dominic
  • 62,658
  • 20
  • 139
  • 163
  • where u called this method **showLoginIfUnauthed** – Anbu.Karthik Apr 20 '16 at 09:40
  • I was thinking of calling it in `func application` and `func applicationDidBecomeActive` but not sure if that's a good way of doing it – Dominic Apr 20 '16 at 09:41
  • Ok we go on step by step , can u show ur storyboard scene – Anbu.Karthik Apr 20 '16 at 09:42
  • Thanks - attached story board, it's pretty much just the default one at the moment with an unconnected LoginViewController – Dominic Apr 20 '16 at 09:46
  • when u loaded the loginviewcontroller ,its load in initially or after after tabbar loaded – Anbu.Karthik Apr 20 '16 at 09:49
  • at the moment the app will just load the first tab (the one saying default). I haven't get put in the code to load the Login controller at all as I'm not sure the best place to put the code which should load it. I tried the code from http://stackoverflow.com/questions/10428629/programatically-set-the-initial-view-controller-using-storyboards/27343365#27343365 and it works to load another view, but I'm not sure if that's a good way – Dominic Apr 20 '16 at 09:51
  • You should consider reversing your logic. The first scene should always be the login view and if the user is authenticated then change the window to the tab bar view, not the other way around. But calling that function your wrote and tweaking it to perform the opposite function will work fine. – pbush25 Apr 20 '16 at 12:49
  • Thanks seems like a safer idea @pbush25 – Dominic Apr 20 '16 at 12:52

0 Answers0