I am stumped by the Facebook API for logging in. I can't find anything helpful on the net.
My FBSessionDelegate methods are not being called, and accessToken and expirationDate values are not being set, so I don't think I am ever logged in.
I reverted to a very simple app, with only two buttons (log in, log out) and a label to see status information.
Here is the code for the view controller, where all the processing takes place:
//
#import <UIKit/UIKit.h>
#import "Facebook.h"
@interface facebook_login_testViewController : UIViewController <FBSessionDelegate>{
UIButton *loginButton;
UIButton *logoutButton;
UILabel *info;
Facebook *facebook;
}
@property (nonatomic, retain) Facebook *facebook;
- (void) loggingIn;
- (void) loggingOut;
@end
and
#import "facebook_login_testViewController.h"
@implementation facebook_login_testViewController
@synthesize facebook;
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[loginButton setTitle: @"Log In" forState:UIControlStateNormal];
[loginButton addTarget:self action:@selector(loggingIn) forControlEvents:UIControlEventTouchUpInside];
loginButton.frame = CGRectMake(200, 200, 200, 50);
logoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[logoutButton setTitle: @"Log Out" forState:UIControlStateNormal];
[logoutButton addTarget:self action:@selector(loggingOut) forControlEvents:UIControlEventTouchUpInside];
logoutButton.frame = CGRectMake(200, 300, 200, 50);
info = [[UILabel alloc] initWithFrame:CGRectMake(200, 400, 400, 600)];
info.numberOfLines = 0;
[self.view addSubview:loginButton];
[self.view addSubview:logoutButton];
[self.view addSubview:info];
info.text = @"Waiting to log in...\n\nPress the login button.";
facebook = [[Facebook alloc] initWithAppId:@"159...........5" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void) loggingIn{
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
}
- (void) loggingOut{
info.text = [NSString stringWithFormat:@"\naccess token: %@\nexpiration date: %@\nfacebood description:%@\n",facebook.accessToken, facebook.expirationDate, facebook];
[facebook logout:self];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
@end
The .plist file is set up like this
This really looks too simple to get wrong. When I click the login button, I can trace through authorize:permission
, and then authorize:permissions localAppId:localAppId
, and as far as I can trace it, I don't see anything that looks like it's going to results in isSessionValid
becoming TRUE.
I wait and then press the log off button, and take a look at some Facebook parameters, in case something asynchronous needed to occur, and they are still null.
Can someone see what I am missing?