0

我试图搜索这个,但找不到任何有用的东西。

[FBSession setActiveSession:[[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:@"publish_actions,read_stream,user_hometown,user_birthday,email", nil]]];

    [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:
    ^(FBSession *session,FBSessionState state,NSError *error)
    {
        if(state == FBSessionStateOpen)
       {
             // use user's detail and post on Facebook
       }
    }];

现在这对我来说很好。但是如果用户在登录 Facebook 之前按下关闭/取消按钮会怎样。如果用户按下取消按钮,我需要执行一组语句。我怎样才能做到这一点。任何帮助,将不胜感激。

4

3 回答 3

5

You can use Facebook error category and to handle errors refer this link

[FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError* error){

        if(!error){

            //success do something
        }
        else{

            //Error 
            if([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled){

                //user have pressed on cancel/close button
            }
            else {

                //loging failed 
            }
        }
    }];
于 2014-02-19T07:34:43.100 回答
1

调用登录函数后,例如:

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {

     NSArray *permissions = [NSArray arrayWithObjects:@"friends_photos",@"friends_birthday",@"email", nil];

     return [FBSession openActiveSessionWithReadPermissions:permissions
                       allowLoginUI:allowLoginUI
                       completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                       [self sessionStateChanged:session state:state error:error];
     }];
}

您可以在委托 meth9od 中获取回调,例如:

- (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState)state
                  error:(NSError *)error
 {

   switch (state) {
     case FBSessionStateOpen:
        if(!error)
        {

        }
        break;
    case FBSessionStateClosed: 
         {
           NSLog(@"FBSessionStateClosed");
           [FBSession.activeSession closeAndClearTokenInformation];
         }
         break;
    case FBSessionStateClosedLoginFailed: 
         {
           NSLog(@"FBSessionStateClosedLoginFailed :- logged failed");
         }
         break;
    default:
        break;
   }

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

   if (error) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Error: %@",
                                                                 [AppDelegate FBErrorCodeDescription:error.code]]
                                                        message:error.localizedDescription
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alertView show];
  }
}
于 2013-08-12T07:26:53.370 回答
1

尝试这个


 [FBSession setActiveSession:[[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:@"publish_actions,read_stream,user_hometown,user_birthday,email", nil]]];

[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:
^(FBSession *session,FBSessionState state,NSError *error)
{
    if(state == FBSessionStateOpen)
   {
         // use user's detail and post on Facebook
   }
   else if(state == FBSessionStateClosed)
  {
       // if user not authenticated
   }
   else if(steate == FBSessionStateClosedLoginFailed)
  {

  }

}];


于 2013-08-12T07:20:59.213 回答