gpt4 book ai didi

ios - 最新的 Facebook SDK 是否会自动检查其应用程序是否已安装?它说它会......但它不会发生在我身上

转载 作者:行者123 更新时间:2023-11-29 12:12:03 31 4
gpt4 key购买 nike

我想提供将图片分享到 Facebook 的选项。当我安装了 FB 应用程序后,我可以毫无问题地发布到我的 feed。但是,当没有安装任何应用程序时,我收到以下错误:

-canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"

Facebook 在文档中说我不需要做任何事情:

Now the SDK automatically checks for the native Facebook app. If it isn't installed, the SDK switches people to their default browser and opens the Feed Dialog. If someone wants to share an Open Graph story, the SDK opens the Web Share Dialog.

我发现要检查的一个 hack 如下:

BOOL isInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fbauth2:/"]];
if (isInstalled) {....}

有没有更好更“正确”的方式?

我分享的报错方法如下:

- (void)showFBShare {

FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
photo.image = self.shareImage;
photo.userGenerated = YES;
FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
content.photos = @[photo];

[FBSDKShareDialog showFromViewController:self
withContent:content
delegate:nil];
}

最佳答案

tl;dr:将 FBSDKShareDialog 实例化并配置为对话框,并设置 dialog.mode = FBSDKShareDialogModeNative 或 FBSDKShareDialogModeShareSheet。 if [dialog canShow] then [dialog show].

FBSDK version used for testing solution:
- FBSDKCoreKit (4.12.0):
- FBSDKShareKit (4.12.0):

在调用 showFromViewController 方法时,该方法似乎没有抛出任何异常,因此排除了 try/catch 修复。但是,如果您实例化 FBSDKShareDialog,则可以调用 canShow 方法来检查应用是否能够在其当前模式(在本例中为 FBSDKShareDialogModeShareSheet)中显示对话框。

Method description from source (FBSDKSharing.h:64):

@abstract A Boolean value that indicates whether the receiver can initiate a share.
@discussion May return NO if the appropriate Facebook app is not installed and is required or an access token is required but not available. This method does not validate the content on the receiver, so this can be checked before building up the content.
@see [FBSDKSharing validateWithError:]
@result YES if the receiver can share, otherwise NO.
- (BOOL)canShow;

代码示例:

...

FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
dialog.fromViewController = self;
dialog.shareContent = content;

/**
* !important: Only mode FBSDKShareDialogModeNative and
* FBSDKShareDialogModeShareSheet do the appropriate checks.
* Otherwise canShow returns yes regardless.
*/
dialog.mode = FBSDKShareDialogModeShareSheet;

if ([dialog canShow]) {
[dialog show];
} else {
// handle exception case.
}

在模拟器上运行时的问题(来自文档):

iOS Simulator and Testing

If you are using Simulator to test sharing in your application, you will see errors if you try to share videos, Photos or Open Graph Actions. This is because you need Facebook for iOS installed which provides the Share Dialog. We do not support this for Simulator.

In the case of link shares, you do not need Facebook for iOS installed so this test case is possible. To test other Sharing scenarios, set up an actual test device with with Facebook for iOS installed.

如上所述,模拟器只支持链接内容。即:

FBSDKShareLinkContent *linkContent = [[FBSDKShareLinkContent alloc] init];
linkContent.contentURL = [NSURL URLWithString:@"somelink"];
linkContent.quote = @"Very interesting, shareworthy link.";

[FBSDKShareDialog showFromViewController:self
withContent:linkContent
delegate:nil];

关于ios - 最新的 Facebook SDK 是否会自动检查其应用程序是否已安装?它说它会......但它不会发生在我身上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33157583/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com