1

您好我在我的项目中使用 facebook SDK 3.8。并在我的应用程序中使用 HelloFaceBookSample 代码,但在我的应用程序中,我没有 facebook 的登录按钮。我已经实现了 facebook 的登录流程,登录后我在 facebook 上发帖。现在发布状态工作正常,但是当我在 facebook 上发布图片时,它给了我错误

an attempt was made reauthorize permissions on an unopened session in ios

代码 :

-(void) clickButtonFacebookUsingSDK
{   
    if (!appdelegate.session.isOpen)
    {       
         appdelegate.session = [[FBSession alloc] init];  


        [appdelegate.session openWithCompletionHandler:^(FBSession *session,
                                                         FBSessionState status,
                                                         NSError *error) {      


             if(appdelegate.session.isOpen)
            {
                NSLog(@"calling postdata when session is not open******");

                 [self postData];
            }

        }];

    }
    else
    {
        NSLog(@"calling postdata when session is  open******");

        [self postData];
    }    
}

-(void) postData
{
    [self showingActivityIndicator];



    UIImage *img = [UIImage imageNamed:@"abc.jpg"];


        [self performPublishAction:^{
            FBRequestConnection *connection = [[FBRequestConnection alloc] init];
            connection.errorBehavior = FBRequestConnectionErrorBehaviorReconnectSession
            | FBRequestConnectionErrorBehaviorAlertUser
            | FBRequestConnectionErrorBehaviorRetry;

            FBRequest *req = [FBRequest requestForUploadPhoto:img];
            [req.parameters addEntriesFromDictionary:[NSMutableDictionary dictionaryWithObjectsAndKeys:message3, @"message", nil]];


            [connection addRequest:req
                 completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

                    // [self showAlert:@"Photo Post" result:result error:error];
                     [self showAlert:@"Photo Post" result:result resulterror:error];
                     if (FBSession.activeSession.isOpen) {
                     }
                 }];
            [connection start];

        }];



}

- (void) performPublishAction:(void (^)(void)) action {
    // we defer request for permission to post to the moment of post, then we check for the permission
    if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound)
    {
        // if we don't already have the permission, then we request it now
        [FBSession.activeSession requestNewPublishPermissions:@[@"publish_actions"]
                                              defaultAudience:FBSessionDefaultAudienceFriends
                                            completionHandler:^(FBSession *session, NSError *error)
                                                {
                                                    if (!error) {
                                                    action();
                                                } else if (error.fberrorCategory != FBErrorCategoryUserCancelled){
                                                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Permission denied"
                                                                                                        message:@"Unable to get permission to post"
                                                                                                       delegate:nil
                                                                                              cancelButtonTitle:@"OK"
                                                                                              otherButtonTitles:nil];
                                                    [alertView show];
                                                }
                                            }];
    } else {
        action();
    }

}


// UIAlertView helper for post buttons
- (void)showAlert:(NSString *)message result:(id)result resulterror:(NSError *)error
  {

    NSString *alertMsg;
    NSString *alertTitle;
    if (error)
    {
        alertTitle = @"Error";
        // Since we use FBRequestConnectionErrorBehaviorAlertUser,
        // we do not need to surface our own alert view if there is an
        // an fberrorUserMessage unless the session is closed.
        if (FBSession.activeSession.isOpen) {
            alertTitle = @"Error";

        } else {
            // Otherwise, use a general "connection problem" message.
            alertMsg = @"Operation failed due to a connection problem, retry later.";
        }
    }
    else
    {
        NSDictionary *resultDict = (NSDictionary *)result;
        alertMsg = [NSString stringWithFormat:@"Successfully posted '%@'.", message];
        NSString *postId = [resultDict valueForKey:@"id"];
        if (!postId) {
            postId = [resultDict valueForKey:@"postId"];
        }
        if (postId) {
            alertMsg = [NSString stringWithFormat:@"%@\nPost ID: %@", alertMsg, postId];
        }
        alertTitle = @"Success";
    }

    if (alertTitle) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
                                                            message:alertMsg
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
        [alertView show];
        [self dismissActivityIndicator];
    }
}

它在 performPublishAction 方法中给了我错误。

谁能告诉我有什么问题。我有很多搜索,也找到了很多解决方案,但没有一个工作。请帮忙。提前致谢。

4

1 回答 1

0

我认为您必须将其活动会话设置为 FBSession :

FBSession *session = [[FBSession alloc] init];
[FBSession setActiveSession:session];
于 2013-10-10T07:43:58.697 回答