Cuchullainn
Cuchullainn

Reputation: 61

How do I go to my main view for my IOS app after logging in with Facebook Connect

I have a problem with connecting to my main view after logging in with the Facebook Connect sdk with my app. After a user logs in I want it to essentially load up the main view of my application (a camera), but so far I can't figure out how to do it. At the moment it just logs in and I am taken to a blank black screen, and I can't reach my view. Before I added the Facebook functionality I could reach my main view no problem.

Below is my appDelegate.h and appDelegate.m code, I can add the viewController code and class if it will help, my xib file is called "ViewController".

appDelegate.h

#import "FBConnect.h"
#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate>

{

    Facebook *facebook;

}

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@property (nonatomic, retain) Facebook *facebook;

@end

appDelegate.m

#import "AppDelegate.h"

#import "ViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize facebook;


- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]   autorelease];
    // Override point for customization after application launch.
    //if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    //    self.ViewController = [[[ViewController alloc] initWithNibName:@"nil" bundle:nil] autorelease];
    //}     

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    facebook = [[Facebook alloc] initWithAppId:@"326350974080015" andDelegate:self];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    if (![facebook isSessionValid]) {
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"user_photos",
                                nil];
        [facebook authorize:permissions];
        [permissions release];
    }    
    return YES;
}

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

    self.ViewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [facebook handleOpenURL:url]; 
}


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [facebook handleOpenURL:url]; 
}

@end

I am also getting this error when I try and run my app:

2012-03-01 21:04:51.103 friendSpotted[619:f803] Applications are expected to have a root     view controller at the end of application launch

I actually have code for this in my appDelegate.m

self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

Upvotes: 2

Views: 1005

Answers (1)

dbrajkovic
dbrajkovic

Reputation: 3703

Before self.window.rootViewController = self.viewController; you need to initialize the view controller self.viewController = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil]; assuming your xib is called "ViewController.xib"

EDIT

Also, I would put all the Facebook related stuff in your view controller not the app delegate

Upvotes: 6

Related Questions