2013-06-12 83 views
2

我試圖讓用戶發佈一個簡單的Facebook狀態更新,而不使用OpenGraph。到目前爲止,允許用戶登錄並要求獲得publish_actions權限,順利進行順利。presentShareDialog返回nil的原因是什麼?

但是,當我嘗試呼叫presentShareDialogWithLink:name:caption:description:picture:clientState:handler:時,它始終返回零並且什麼也沒有顯示。它似乎甚至沒有打電話給處理程序,不知道爲什麼它不起作用。

這可能會失敗的原因是什麼?如果我知道可能是什麼原因,我總是可以追溯到我的步驟。

相關代碼

用戶按下按鈕

AppDelegate_Pad *appDelegate = (AppDelegate_Pad *)[[UIApplication sharedApplication] delegate]; 
if(FBSession.activeSession.isOpen) { 
    [appDelegate postFacebookUpdateWithAlbum:album]; 
} else { 
    // The person using the app has initiated a login, so call the openSession method 
    // and show the login UX if necessary. 
    [appDelegate openSessionWithAllowLoginUI:YES album:album]; 
} 

Facebook的代碼在應用程序代理

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{ 
    if ([FBSession.activeSession handleOpenURL:url]) { 
     [self postFacebookUpdateWithAlbum:self.facebookAlbum]; 
     self.facebookAlbum = nil; 
     return YES; 
    } 
} 

#pragma mark Facebook 

    /* 
    * Opens a Facebook session and optionally shows the login UX. 
    */ 
    - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI album:(LPAlbum *)album { 
     self.facebookAlbum = album; 

     return [FBSession openActiveSessionWithPublishPermissions:@[@"publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { 
      [self sessionStateChanged:session 
           state:status 
           error:error]; 

     }]; 
    } 

    - (void)postFacebookUpdateWithAlbum:(LPAlbum *)album { 
     if (album) { 
      // 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) { 
                  [self doPostWithAlbum:album]; 
                 } 
                 else { 
                  DLog(@"Could not get permissions. Error: %@", error); 
                 } 
                }]; 
      } else { 
       [self doPostWithAlbum:album]; 
      } 
     } 
    } 

    - (void)doPostWithAlbum:(LPAlbum *)album { 
     NSString *initialText = [NSString stringWithFormat:@"Neat status update"]; 
     NSURL *coverImageURL = [NSURL URLWithString:album.imageBaseURLString]; 
     UIImage *coverImage = [UIImage imageWithContentsOfFile:album.imageBaseURLString]; 
     NSURL *infoLinkURL = [NSURL URLWithString:album.infoSiteURLString]; 

     // First, try the iOS 6 native sharing. 
     BOOL iOS6native = [FBDialogs presentOSIntegratedShareDialogModallyFrom:self.window.rootViewController initialText:initialText image:coverImage url:infoLinkURL handler:^(FBOSIntegratedShareDialogResult result, NSError *error) { 
     }]; 

     if (!iOS6native) { 
      NSString *name = @"name"; 
      NSString *caption = @"caption"; 
      NSString *description = @"description"; 

      FBShareDialogParams *params = [[FBShareDialogParams alloc] init]; 
      params.link = infoLinkURL; 
      params.name = name; 
      params.description = description; 

      BOOL canPresent = [FBDialogs canPresentShareDialogWithParams:params]; // returns NO 
      DLog(@"Can present with params? %@", canPresent ? @"Yes" : @"No"); 
      canPresent = [FBDialogs canPresentOSIntegratedShareDialogWithSession:[FBSession activeSession]]; // returns NO 
      DLog(@"Can present with session? %@", canPresent ? @"Yes" : @"No"); 

      FBAppCall *appCall = [FBDialogs presentShareDialogWithLink:infoLinkURL 
                    name:name 
                   caption:caption 
                  description:description 
                   picture:coverImageURL 
                  clientState:nil 
                   handler:^(FBAppCall *call, NSDictionary *results, NSError *error) { 
                    if (error) { 
                     DLog(@"Error: %@", error.description); 
                    } else { 
                     DLog(@"Success!"); 
                    } 
                   }]; 
      if (!appCall) { 
       // App call failed. 
      } 
     } 
    } 

    // Convenience method to perform some action that requires the "publish_actions" permissions. 
    - (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(); 
                } 
                //For this example, ignore errors (such as if user cancels). 
               }]; 
     } else { 
      action(); 
     } 

    } 
+0

如果您沒有Facebook應用的正確版本,則返回nil。你在模擬器上得到這個嗎?不幸的是,您只能通過應用商店中的最新Facebook應用在設備上測試此功能。 –

+0

等一下,你真的需要這個Facebook應用程序嗎?我的印象是,Share Dialog是本地系統對話框的替代品,僅在iOS 6及更高版本中可用。 – SpacyRicochet

回答

2

presentShareDialogWithLink需要Facebook的應用程序(它的記錄here,並說「在Facebook應用程序中提供一個對話框......」)。

如果你想使用iOS6系統對話框,你需要一個presentOSIntegratedShareDialogModallyFrom方法,如this one

+0

謝謝,我完全誤解了'在Facebook應用程序中'。我認爲這意味着你正在製作的應用程序,因爲他們也稱這是一個Facebook應用程序。 – SpacyRicochet

+0

啊,我可以看到混亂。 –

相關問題