Ios 为什么FBSDK共享对话框不能与图像附件一起使用?

Ios 为什么FBSDK共享对话框不能与图像附件一起使用?,ios,swift,facebook,facebook-ios-sdk,facebook-share,Ios,Swift,Facebook,Facebook Ios Sdk,Facebook Share,我想在Facebook上分享图片,为此我做了: let photo: FBSDKSharePhoto = FBSDKSharePhoto() photo.image = croppedImage photo.userGenerated = true photo.caption = "Add Your caption" let content: FBSDKSharePhotoContent = FBSDKSharePhotoContent() content.photos = [photo]

我想在Facebook上分享图片,为此我做了:

let photo: FBSDKSharePhoto = FBSDKSharePhoto()
photo.image = croppedImage
photo.userGenerated = true
photo.caption = "Add Your caption"

let content: FBSDKSharePhotoContent = FBSDKSharePhotoContent()
content.photos = [photo]

FBSDKShareDialog.showFromViewController(self, withContent: content, delegate: nil)
但它没有显示ShareViewController。但当我尝试这段代码时:

let content: FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "http://")
content.contentTitle = "App Testing"
content.contentDescription = "I'm working over the app!"

如何修复第一个代码段?

我知道这是一个老问题,但以防有人面临同样的问题:

根据,您需要安装本机Facebook应用程序才能使用
FBSDKSharePhotoContent
,在这种情况下,您不能使用
FBSDKShareDialog

实现几乎相同结果的一个解决办法是使用
FBSDKShareDialog
(它只支持
FBSDKShareLinkContent
),如下所示:

let content: FBSDKShareLinkContent = FBSDKShareLinkContent()
content.imageURL = URL(string: "www.example.com/my_image.jpg")
content.contentURL = URL(string: "www.example.com/target_url")
content.contentTitle = "My Title"
content.contentDescription = "My Description"

let dialog: FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self      // make sure to conform to FBSDKSharingDelegate protocol
dialog.shareContent = content
if !dialog.canShow() {
    // fallback to non-native mode
    dialog.mode = .feedBrowser
}
dialog.show()

您可以找到协议的方法。

它仅在安装Facebook应用程序时有效。另外,明确指定
mode=.native

let content = FBSDKSharePhotoContent()
    let photo = FBSDKSharePhoto(image: image, userGenerated: true)
    content.photos = [photo].flatMap({ $0 })

    let dialog = FBSDKShareDialog()
    dialog.fromViewController = viewController
    dialog.shareContent = content
    dialog.delegate = self
    dialog.mode = .native

    dialog.show()
如果没有,它将使用
SLComposeViewController
,您将得到错误消息

SLComposeViewController isAvailableForServiceType got serviceType com.apple.social.facebook isAvailable 0
好的做法是在
FBSDKShareDialog.m
上进行调试,看看事情是否适合您

- (BOOL)show
{
  BOOL didShow = NO;
  NSError *error = nil;

  if ([self _validateWithError:&error]) {
    switch (self.mode) {
      case FBSDKShareDialogModeAutomatic:{
        didShow = [self _showAutomatic:&error];
        break;
      }
      case FBSDKShareDialogModeBrowser:{
        didShow = [self _showBrowser:&error];
        break;
      }
      case FBSDKShareDialogModeFeedBrowser:{
        didShow = [self _showFeedBrowser:&error];
        break;
      }
      case FBSDKShareDialogModeFeedWeb:{
        didShow = [self _showFeedWeb:&error];
        break;
      }
      case FBSDKShareDialogModeNative:{
        didShow = [self _showNativeWithCanShowError:&error validationError:&error];
        break;
      }
      case FBSDKShareDialogModeShareSheet:{
        didShow = [self _showShareSheetWithCanShowError:&error validationError:&error];
        break;
      }
      case FBSDKShareDialogModeWeb:{
        didShow = [self _showWeb:&error];
        break;
      }
    }
  }
  if (!didShow) {
    [self _invokeDelegateDidFailWithError:error];
  } else {
    [self _logDialogShow];
    [FBSDKInternalUtility registerTransientObject:self];
  }
  return didShow;
}
经验教训:

  • 注意FB的变化
  • 每天早上检查更改日志
  • 今天起作用的东西明天可能不起作用

您是否安装了Facebook应用程序
FBSDKSharePhoto
需要该应用程序。From:“人们需要安装iOS应用程序的本机Facebook,7.0或更高版本”@ama1111当然
FBSDKShareLinkContent
起作用,但
FBSDKSharePhotoContent
-若不获取更多信息,您可以实现
FBSDKSharingDelegate
,从视图控制器传递到
showFromViewController
,并查看它为
didFailWithError
@ama1111报告的内容。您能帮我举个例子吗?实现
FBSDKSharingDelegate
;SDK中将提到的内容属性标记为已弃用