Facebook iOS SDK 4.1.0共享回调 [英] Facebook iOS SDK 4.1.0 Share Callback

查看:450
本文介绍了Facebook iOS SDK 4.1.0共享回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此问题标题中提到的F​​BSDK,我在视图控制器中显示一个简单的共享对话框:

Using the FBSDK mentioned in the title of this question, I present a simple share dialog in a view controller:

// Setup the content for the share
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = linkUrl;
content.contentDescription = description;
content.contentTitle = title;
content.imageURL = imageUrl;

// Show the share dialog
[FBSDKShareDialog showFromViewController:controller
                             withContent:content
                                delegate:someDelegate];

并实施委托方法......

And implement the delegate method...

- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results
{
    NSLog(@"Sweet, they shared.");
}

到目前为止,只要用户安装了Facebook应用程序就可以了他们的设备。当用户没有安装Facebook时会出现问题。如果是这种情况,Safari会打开Facebook登录流程的Web版本(这很好),但是如果您在没有登录Facebook /执行任何其他任务的情况下切换回原始应用程序,则上面显示的完成委托方法是叫做。

So far so good, as long as the user has the Facebook application installed on their device. The issue arises when the user does not have Facebook installed. If this is the case, Safari opens up to a web version of Facebook's login flow (this is fine), but if you then switch back to the original application without logging into Facebook / performing any additional tasks, the completion delegate method shown above is called.

有没有人有解决这个问题的经验?似乎应该有一种可靠的方法来确定帖子是否确实发生。

Does anyone have any experience with a workaround for this? It seems like there should be a reliable way for determining whether or not the post did indeed occur.

注意:上面的代码非常伪。在实际实现中,我确实实现了所有委托回调(didComplete,didCancel和didFail)。

Note: The above code is pretty pseudo-ish. In the actual implementation I have indeed implemented all of the delegate call backs (didComplete, didCancel, and didFail).

推荐答案

编辑:事实证明,如果在设备上安装了Facebook应用程序,则在调用完成方法时结果字典为空。为了解决这个问题,需要检查是否首先安装了Facebook。



当然,在发布后我偶然发现了答案。如果共享实际发生,didCompleteWithResults方法中返回的结果字典包含postId键。所以逻辑很简单:

It turns out, the results dictionary is empty when the completion method is called if the Facebook app is installed on the device. To overcome this, a check needs to be done to see if Facebook is installed first.

Of course after posting I stumbled upon the answer. The results dictionary returned in the didCompleteWithResults method contains a postId key if the share actually occurred. So the logic is as simple as:

- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results
{
    NSURL *fbURL = [NSURL URLWithString:@"fb://"];
    if (![[UIApplication sharedApplication] canOpenURL:fbURL])
        if (results[@"postId"]) {
            NSLog(@"Sweet, they shared, and Facebook isn't installed.");
        } else {
            NSLog(@"The post didn't complete, they probably switched back to the app");
        }
    } else {
        NSLog(@"Sweet, they shared, and Facebook is installed.");
    }
}

虽然这有效,但似乎不是如果Facebook将密钥从postId更改为未来的其他内容会怎么样呢?不太可能,但是你明白了我的观点。

Although this works, it doesn't seem to be a very safe way of going about things (what if Facebook changes the key from "postId" to something else in the future? Unlikely but you get my point).

这篇关于Facebook iOS SDK 4.1.0共享回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆