如何检查是否已登录ios中的Facebook?

时间:2013-08-20 10:34:50

标签: iphone ios facebook facebook-graph-api

我正在使用“Graph Api”在我的应用程序中登录Facebook。我成功登录并使用此代码注销

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

我使用以下代码登录已经登录的

 if (![facebook isSessionValid]) {

    [facebook authorize:permissions ];
 }

我的问题是我安装Facebook App,我通过Facebook App登录。然后我运行我的应用程序,它再次要求登录Facebook。我是如何解决这个问题的。它应该作为默认的FB应用程序登录凭据。我看到许多应用程序使用此功能。任何人都知道解决方案

1 个答案:

答案 0 :(得分:2)

  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;