presentShareDialog 返回 nil 的原因是啥?

Posted

技术标签:

【中文标题】presentShareDialog 返回 nil 的原因是啥?【英文标题】:What are the reasons that presentShareDialog returns nil?presentShareDialog 返回 nil 的原因是什么? 【发布时间】:2013-06-12 12:48:18 【问题描述】:

我试图让用户发布一个简单的 Facebook 状态更新,而不使用 OpenGraph。到目前为止,允许用户登录并请求publish_actions 权限,一切顺利。

但是,当我尝试调用 presentShareDialogWithLink:name:caption:description:picture:clientState:handler: 时,它总是返回 nil 并且什么也不显示。它甚至似乎都没有调用处理程序,这使得它不知道为什么它不起作用。

这可能失败的原因是什么?如果我知道可能是什么原因,我总是可以原路返回。

相关代码

用户按下按钮

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];

App Delegate 中的 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();
        

    

【问题讨论】:

如果您没有正确版本的 Facebook 应用程序,它将返回 nil。你是在模拟器上得到这个吗?遗憾的是,您只能在设备上使用应用商店中最新的 Facebook 应用测试此功能。 等等,你真的需要 Facebook 应用程序吗?我的印象是共享对话框是本机系统对话框的替代品,仅在 iOS 6 及更高版本中可用。 【参考方案1】:

presentShareDialogWithLink 需要 Facebook 应用程序(记录在 here,并说“在 Facebook 应用程序中显示对话框......”)。

如果您想使用 iOS6 系统对话框,您需要使用 presentOSIntegratedShareDialogModallyFrom 方法之一,例如 this one。

【讨论】:

谢谢,我完全误解了“在 Facebook 应用程序中”。我以为它指的是你目前正在制作的应用,因为他们也称它为 Facebook 应用。 啊,我可以看出混乱了。

以上是关于presentShareDialog 返回 nil 的原因是啥?的主要内容,如果未能解决你的问题,请参考以下文章

Lua 函数如何返回 nil,即使函数内部的返回值不是 nil?

URLForUbiquityContainerIdentifier 总是返回 nil

为啥 FileHandle 不一致地返回“nil”

为啥 NSFormatter dateFromString 偶尔会返回 nil?

WKImage 总是返回 nil

为啥我的 tableView 变量返回 nil?