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.