0

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

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

我使用下面的代码已经登录了 facebook

 if (![facebook isSessionValid]) {

    [facebook authorize:permissions ];
 }

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

4

1 回答 1

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;
于 2013-08-20T11:06:39.053 回答