FBSession:尝试重新授权ios中未打开的会话的权限

时间:2013-10-10 06:27:08

标签: ios facebook

您好我在我的项目中使用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方法中给出了错误。

任何人都可以告诉我这是什么问题。我有很多搜索,也找到了很多解决方案,但没有人工作。请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为你必须设置为FBSession它的活动会话是什么:

FBSession *session = [[FBSession alloc] init];
[FBSession setActiveSession:session];