In my new iOS App I have to login to Facebook and fetch user details. I have read the tutorial here - http://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/authenticate/ and studied the Scrumptious and other Samples. But I am not able to understand the flow.
I have created a sample project to do Facebook login (code below) - but have below main question :
- Why does the App asks for authorisation when I click on the Login button again - after I click "OK" on Facebook once
Below is my code -
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// See if the app has a valid token for the current state.
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded)
{
// To-do, show logged in view --- LOGGED IN : Already Login
// Yes, so just open the session (this won't display any UX).
UIWindow *window = [UIApplication sharedApplication].keyWindow;
ViewController *rootViewController = (ViewController *)window.rootViewController;
[rootViewController openSession];
}
return YES;
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBSession.activeSession handleDidBecomeActive];
}
@implementation ViewController
@synthesize lblStatus;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
lblStatus.text = @"Login Now";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)login:(id)sender
{
[self openSession];
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
{
// Connection is Open
lblStatus.text = @"FBSessionStateOpen";
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
{
[FBSession.activeSession closeAndClearTokenInformation];
// Connection is Closed / Login Failed
lblStatus.text = @"FBSessionStateClosed";
}
break;
default:
break;
}
}
- (void)openSession
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}
ANSWER ---->
Thanks nerowolfe for the suggestions.
Below is my complete solution to connect to Facebook. My next task will be to get the user data now.
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBSession.activeSession handleDidBecomeActive];
}
--------------------------
@implementation ViewController
@synthesize lblStatus;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
lblStatus.text = @"Login Now";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)login:(id)sender
{
[self openSession];
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
{
// Connection is Open
lblStatus.text = @"FBSessionStateOpen";
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
{
[FBSession.activeSession closeAndClearTokenInformation];
// Connection is Closed / Login Failed
lblStatus.text = @"FBSessionStateClosed";
}
break;
default:
break;
}
}
- (void)openSession
{
if(FBSession.activeSession.isOpen)
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:NO
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}
else
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}
}
- (IBAction)logout:(id)sender
{
[FBSession.activeSession closeAndClearTokenInformation];
if(FBSession.activeSession.isOpen)
lblStatus.text = @"Open Still";
else
lblStatus.text = @"Close";
}