2013-10-10 60 views
1

您好,我在我的項目中使用了Facebook SDK 3.8。並在我的應用程序中使用HelloFaceBookSample代碼,但在我的應用程序中,我沒有Facebook的登錄按鈕。我已經實現了Facebook的登錄流程,登錄後我有在Facebook上發佈。現在的狀態後工作正常,但是當我在Facebook上發佈圖片它給我FBSession:試圖對ios中未打開的會話重新授權權限

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方法。

誰能告訴我是什麼問題。我有很多搜索,也發現了很多解決方案,但沒有人工作。請幫忙。提前致謝。

回答

0

我認爲你必須設置爲FBSession什麼是它的活動會話:

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

喔感謝。我已經用相同的解決方案解決了這個問題。 –

+0

你能告訴我一件事...我想在應用程序頁面上張貼圖像。怎麼能做到這一點。任何線索。謝謝 –

+0

對不起,從來沒有做過這樣的事情:( – KIDdAe