Gabriel
Gabriel

Reputation: 2461

Use storyboard instead of .xib files in iOS for Facebook connect

I am starting an app from scratch in iOS, that uses a Facebook login. I cannot find a tutorial using the storyboard; they all use .xib files, even on the Facebook tutorial.

Is it recommended to use .xib files for a Facebook connect app? If yes, why?

Upvotes: 1

Views: 588

Answers (2)

Gabriel
Gabriel

Reputation: 2461

Ok, finally it is not so hard. I find this: selecting alternative first view controller from story board at application startup It's pretty similar, so this is my personal implementation.

Create two views in the storyboard, for me they were named SCViewController and SCLoginViewController (I put the same class name and Storyboard ID). Then add a navigation controller, and put the arrow that points to the first element displayed on the Navigation Controller.

Than, in the app delegate implementation file, add this to the didFinishLaunchingWithOptions function:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // See if the app has a valid token for the current state.
    if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
        // Yes, so just open the session (this won't display any UX).
        [self openSession];
    } else {
        // No, display the login page.
        [self showLoginView];
    }

    return YES;
}

And add the function:

- (void)showLoginView
  {
            NSBundle *bundle = [NSBundle mainBundle];
            NSString *sbFile = [bundle objectForInfoDictionaryKey:@"UIMainStoryboardFile"];
            UIStoryboard *sb = [UIStoryboard storyboardWithName:sbFile bundle:bundle];
            UIViewController *rootController;
            rootController = [sb instantiateViewControllerWithIdentifier:@"SCLoginViewController"];
            [self.window setRootViewController:rootController];
  }

If you are logged when you open the app, you arrive on SCViewController, otherwise you arrive on SCLoginViewController.

You can then follow the iOS tutorial from Facebook (app Scrumptious) as if you were using .xib files: https://developers.facebook.com/docs/ios/ios-sdk-tutorial/.

Don't forget to create the following properties:

@property (strong, nonatomic) UINavigationController* navController;
@property (strong, nonatomic) SCViewController *mainViewController;

and to import:

#import <FacebookSDK/FacebookSDK.h>
#import "SCLoginViewController.h"
#import "SCViewController.h"

Upvotes: 1

JonahGabriel
JonahGabriel

Reputation: 3094

It doesn't really matter if you use .xib files or storyboards, the end result is the same. If you want some info on how to adopt storyboards, check out this link from the Apple documentation.

Upvotes: 1

Related Questions