使用会话处理Facebook iOS中的取消button

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

[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 } }]; 

现在这对我来说工作得很好。 但是,如果用户在loginFacebook之前按下closures/取消button,该怎么办? 如果用户按下取消button,我需要执行一组语句。 我怎样才能做到这一点。 任何帮助,将不胜感激。

尝试这个

  [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) { } }]; 

您可以使用Facebook错误类别和处理错误引用此链接

 [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 } } }]; 

在调用loginfunction后,例如:

 - (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]; }]; } 

您可以在委托方法中获得callback,例如:

 - (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]; } }