2013-08-12 23 views
0

我试图搜索这个,但找不到有用的东西。处理取消按钮在facebook iOS使用会话

[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之前按下关闭/取消按钮会怎么样。如果用户按下取消按钮,我需要执行一组语句。我怎样才能做到这一点。 任何帮助,将不胜感激。

回答

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) 
    { 

    } 

}]; 


+0

它的NT工作。 – keen

+0

尝试编辑代码 –

+0

是的,我已经找到了这个解决方案。和它的工作。 – keen

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]; 
    } 
} 
+0

它帮助,但它不是我想要的。我会尽快回复。感谢您的帮助。 – keen

5

您可以使用Facebook error category和处理错误请参阅本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 
      } 
     } 
    }]; 
相关问题