banu
banu

Reputation: 787

how to check if already logged in Facebook in ios?

I am using "Graph Api" For Facebook login with in my app. I successfully logIn and logged out with this code

    facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self.viewController];

and i using below code for already facebook logged in

 if (![facebook isSessionValid]) {

    [facebook authorize:permissions ];
 }

My problem is i install Facebook App, and i logged in via Facebook App. then i run my app, it ask again login Facebook. how i solve this issue. it should take as a default FB app login credentials. i saw many app using this feature. anyone know the solution

Upvotes: 0

Views: 1772

Answers (1)

Sarfaraz Khan
Sarfaraz Khan

Reputation: 592

  1. In your AppDelegate.m file

define NSString *const FBSessionStateChangedNotification =
          @"com.example:FBSessionStateChangedNotification";
replace com.example with your bundle identifier name

2.- In method
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [FBSession setActiveSession:[FBSession new]];
    if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded)
    {
        // means u are already logged in
        //do ur code
        [self openSessionWithAllowLoginUI:NO];
    }
    else
   {      //do ur code
            [self openSessionWithAllowLoginUI:YES];
   }
}
3. whenenever and wherever this method is called after end of switch case write
  - (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
 {

 [[NSNotificationCenter defaultCenter]
     postNotificationName:FBSessionStateChangedNotification
     object:session];
}

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
    return [FBSession openActiveSessionWithReadPermissions:@"email"
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,      FBSessionState state, NSError *error)
            {
                [self sessionStateChanged:session state:state error:error];
            }];
}

Sorry in your appDelegate.h file it should be
extern NSString *const FBSessionStateChangedNotification;

Upvotes: 2

Related Questions