简体   繁体   中英

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

  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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM