简体   繁体   中英

Facebook SDK error 3 and error 2

I have this application I am using for posting on facebook and I am currently facing difficulties in posting on some of the iOS 6.0 devices. I am using facebook SDK 3.1 only and trying to publish action. Following is the code I am using in the class to initiate the read permission.

For the access I am using the following code.

// CALLING THIS CODE BLOCK IN ONE BUTTON ACTION.
        if (FBSession.activeSession.isOpen)
        {
            [self pickaChoice];
        } 
        else 
        {
            [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects:@"publish_actions", nil]
                                               defaultAudience:FBSessionDefaultAudienceEveryone
                                                  allowLoginUI:YES
                                             completionHandler:
             ^(FBSession *session,
               FBSessionState state, NSError *error) {
                 [self sessionStateChanged:session state:state error:error];
             }];
        }

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

    switch (state)
    {
        case FBSessionStateOpen:
            [FBSession setActiveSession:session];
            [self pickaChoice];


            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            // Once the user has logged in, we want them to
            // ...

            [FBSession.activeSession closeAndClearTokenInformation];

            break;
        default:
            break;
    }

    if (error) {
        NSString* message = [NSString stringWithFormat:@"You have disallowed application to post on your behalf."];
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:message
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
        [FBSession.activeSession closeAndClearTokenInformation];
    }
}


-(void)pickaChoice
{
    /* Just a class to select message and then retrieve the message in the next function 
    -(void)didSelectaPhraseToPost:(NSString *)message   */
    FBPublishViewController *fbPublishViewController = [[FBPublishViewController alloc] initWithNibName:@"FBPublishViewController"
                                                                                                 bundle:[NSBundle mainBundle]];

    fbPublishViewController.selectionDelegate = self;

    [self presentViewController:fbPublishViewController
                       animated:YES
                     completion:^(){
                         //nil
                     }];

}

-(void)didSelectaPhraseToPost:(NSString *)message
{
    // Selecting a message from a class and retrieving here. This is the message to post on the feed. 

    [self publishMessage:message];
}

- (void) performPublishAction:(void (^)(void)) action 
{

    if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
        [FBSession.activeSession reauthorizeWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                                   defaultAudience:FBSessionDefaultAudienceFriends
                                                 completionHandler:^(FBSession *session, NSError *error) {
                                                     if (!error) {
                                                         action();
                                                     }
                                                     //For this example, ignore errors (such as if user cancels).
                                                 }];
    } else {
        action();
    }

}

- (void)publishMessage:(NSString *)message
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"APP_NAME", @"name",
                                   message, @"message",
                                   APP_LINK, @"link",
                                   @"APP_PICTURE", @"picture",
                                   nil];

     [self.spinner startAnimating];

       [self performPublishAction:^{
            [FBRequestConnection
             startWithGraphPath:@"me/feed"
             parameters:params
             HTTPMethod:@"POST"
             completionHandler:^(FBRequestConnection *connection,
                                 id result,
                                 NSError *error) {


                 [self.spinner stopAnimating];

                 NSString *messageTitle = nil;

                 NSString *message = nil;

                 // If the result came back okay with no errors...
                 if (result && !error)
                 {
                     NSLog(@"accessToken : %@",[FBSession activeSession].accessToken );
                     NSLog(@"result : %@",result);
                     messageTitle = @"Success";
                     message = @"App has posted to facebook";

                 }else{
                     NSLog(@"error : %@",error);
                     messageTitle = @"Error v1.1";
                     //message =  error.localizedDescription;
                     message = @"Unable to process the request. Please check the permissions for the application.";
                     [FBSession.activeSession closeAndClearTokenInformation];
                 }

                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:messageTitle
                                                                 message:message
                                                                delegate:nil
                                                       cancelButtonTitle:@"OK"
                                                       otherButtonTitles: nil];
                 [alert show];

                 //TODO maybe clear connection here if we want to force an new login 

             }];
       }];

}

Now the problem is on some iOS 6.0 devices it is throwing facebook.sdk.error 3 and on some devices it is throwing facebook.sdk.error 2 even when the application is permitted to post. In the current code I have just changed the message to a custom for more user friendly message but if you go on to print the localizedDescription it will show those.

On most of the iOS 6.0 devices the code is working absolutely fine and the message is posted. Let me know if anyone can find out where the problem exactly is. I have spent like days now in this and still not getting where the problem is.

Edit 1 A pattern I observed that when facebook application is installed and user is logged in through it and not logged in through the native settings I am facing these sort of difficulties.

如果您找到自己的iOS版本,并为iOS 6.0使用Apple的默认Facebook shar视图,那么对于另一版本,则必须使用Graph API。

I used this method its work for me fine in iOS6, may be its help you.

-(void)openFacebookAuthentication
{
    NSArray *permission = [NSArray arrayWithObjects:kFBEmailPermission,kFBUserPhotosPermission, nil];

    [FBSession setActiveSession: [[FBSession alloc] initWithPermissions:permission] ];

    [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {

        switch (status) {
            case FBSessionStateOpen:
                [self getMyData];
                break;
            case FBSessionStateClosedLoginFailed: {
                NSString *errorCode = [[error userInfo] objectForKey:FBErrorLoginFailedOriginalErrorCode];
                NSString *errorReason = [[error userInfo] objectForKey:FBErrorLoginFailedReason];
                BOOL userDidCancel = !errorCode && (!errorReason || [errorReason isEqualToString:FBErrorLoginFailedReasonInlineCancelledValue]);


                if(error.code == 2 && ![errorReason isEqualToString:@"com.facebook.sdk:UserLoginCancelled"]) {
                    UIAlertView *errorMessage = [[UIAlertView alloc] initWithTitle:kFBAlertTitle
                                                                           message:kFBAuthenticationErrorMessage
                                                                           delegate:nil
                                                                           cancelButtonTitle:kOk
                                                                           otherButtonTitles:nil];
                    [errorMessage performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
                    errorMessage = nil;
                }

            }
                break;
                // presently extension, log-out and invalidation are being implemented in the Facebook class
            default:
                break; // so we do nothing in response to those state transitions
        }
    }];
    permission = nil;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM