使用本问题标题中提到的 FBSDK,我在 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];
并实现委托(delegate)方法...
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results
{
NSLog(@"Sweet, they shared.");
}
到目前为止一切顺利,只要用户在其设备上安装了 Facebook 应用程序即可。当用户没有安装 Facebook 时就会出现此问题。如果是这种情况,Safari 将打开 Facebook 登录流程的 Web 版本(这很好),但如果您随后切换回原始应用程序而不登录 Facebook/执行任何其他任务,则上面显示的完成委托(delegate)方法是已调用。
有人有解决此问题的经验吗?看来应该有一个可靠的方法来确定该帖子是否确实发生过。
注意:上面的代码相当伪。在实际实现中,我确实实现了所有委托(delegate)回调(didComplete、didCancel 和 didFail)。
最佳答案
编辑:事实证明,如果设备上安装了 Facebook 应用程序,则调用完成方法时结果字典为空。为了解决这个问题,需要首先检查 Facebook 是否已安装。
当然,在发布后我偶然发现了答案。如果共享实际发生,则 didCompleteWithResults 方法中返回的结果字典包含 postId 键。所以逻辑很简单:
- (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 将来将 key 从“postId”更改为其他内容怎么办?不太可能,但你明白我的意思)。
关于ios - Facebook iOS SDK 4.1.0 分享回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31169965/