2013-03-15 69 views
1

的Facebook SDK 3.2的Facebook牆上張貼剛過Facebook的註冊iOS-的Facebook SDK 3.2

我只需要登錄與功能NEW後張貼在Facebook的牆後。

這裏是我使用的代碼:

這裏我有一個打開的會話的Facebook:

- (IBAction)FBSignup:(id)sender { 
    [[AppDelegate sharedObject] openSession:^(FBSession *session, FBSessionState state, NSError *error) { 
     [self sessionStateChanged:session state:state error:error]; 
    }]; 
} 

狀態改變,我填充數據與[自populateUserDetails],然後試圖張貼在Facebook牆上以及[self postOnFB],但沒有張貼在Facebook牆上。

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

      [self populateUserDetails]; 
      [self postOnFB]; 
      break; 

     case FBSessionStateClosed: 
     case FBSessionStateClosedLoginFailed: 
      [FBSession.activeSession closeAndClearTokenInformation]; 
      break; 
     default: 
      break; 
    } 

    [[NSNotificationCenter defaultCenter] 
    postNotificationName:SCSessionStateChangedNotification 
    object:session]; 

    if (error) { 
     UIAlertView *alertView = [[UIAlertView alloc] 
            initWithTitle:@"Error" 
            message:error.localizedDescription 
            delegate:nil 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 
     [alertView show]; 
    } 
} 

在這裏,我彈出與facebook用戶對象,我需要的數據。

- (void)populateUserDetails 
{ 
    if (FBSession.activeSession.isOpen) { 

     [[FBRequest requestForMe] startWithCompletionHandler: 
     ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { 

      if (!error) { 
       /////////////////////////////// 
       // Populating Data Where Needed 
       /////////////////////////////// 


       // [self postOnFB]; 
      } 
     }]; 
    } 
} 

在這裏,我嘗試張貼的牆後與[自postOnFB]提示錯誤:

Terminating app due to uncaught exception 'com.facebook.sdk:InvalidOperationException', reason: 'FBSession: It is not valid to reauthorize while a previous reauthorize call has not yet completed.

我在這裏張貼在牆上。對於Facebook壁交

- (void)postOnFB{ 

    // Ask for publish_actions permissions in context 
    if ([FBSession.activeSession.permissions 
     indexOfObject:@"publish_actions"] == NSNotFound) { 
     // No permissions found in session, ask for it 
     [FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:@"publish_actions"] 
               defaultAudience:FBSessionDefaultAudienceFriends 
              completionHandler:^(FBSession *session, NSError *error) { 
               if (!error) { 
                [self fbPostSettings]; 
               } 
              }]; 
    } else { 
     // If permissions present, publish the story 
     [self fbPostSettings]; 
    } 
} 

設置參數:

- (void)fbPostSettings{ 

NSDictionary * facebookParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys: 
    kFacebookPostLink,        @"link", 
    kFacebookPostImage,       @"picture", 
    kFacebookPostName,        @"name", 
    kFacebookPostCaption,       @"caption", 
    kFacebookPostDescription,      @"description", 
    nil]; 

    [FBRequestConnection startWithGraphPath:@"me/feed" 
           parameters:facebookParams 
           HTTPMethod:@"POST" 
          completionHandler:^(FBRequestConnection *connection, 
               id result, 
               NSError *error) { 
           NSString *alertText; 
           if (error) { 
            alertText = [NSString stringWithFormat: 
               @"error: domain = %@, code = %d", 
               error.domain, error.code]; 
           } else { 
            alertText = [NSString stringWithFormat: 
               @"Posted action, id: %@", 
               [result objectForKey:@"id"]]; 
           } 
           NSLog(@"%@",alertText); 
          }]; 
} 

- (void)sessionStateChanged:(NSNotification*)notification { 
    [self populateUserDetails]; 
} 

如果我打電話postOnFB一些按鈕操作選擇器(通過Facebook用戶對象數據羣結束後),它貼在所述壁細。但我需要發佈它後,我得到startWithCompletionHandler: ^(FBRequestConnection *連接,NSDictionary *用戶,NSError *錯誤)方法的用戶對象。

請幫忙。謝謝大家:)

+0

看到這個:http://stackoverflow.com/questions/15180991/facebook -sdk -fbloginview-reauthorizating-for-publish-stream/15185899#15185899 – Vishal 2013-03-16 05:00:17

回答

6

不是打開會話,然後請求發佈權限,直接要求發佈的權限打開該會話

[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects:@"publish_stream", 
                nil] defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session, 
    FBSessionState state, NSError *error) { 
if (FBSession.activeSession.isOpen && !error) { 
    [self fbPostSettings]; 
}]; 
+0

感謝Peter,它做到了:) – 2013-03-16 19:01:39

+1

在SDK 3.2+中,必須單獨請求讀寫權限。 – Zorayr 2014-04-29 03:13:13

+0

@Zorayr - 你能解釋一下嗎?那麼「openActiveSessionWithPublishPermissions」是做什麼的?這種方法似乎不適用於我,所以我試圖瞭解我錯過了什麼 – Oren 2015-01-18 21:03:40