John Doe
John Doe

Reputation: 821

Why FBSDK Share Dialog does not work with image attachment?

I want to share image on Facebook, for this I do:

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)

but it does not show me ShareViewController. But when I'm trying this code:

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

How can I fix the first snippet?

Upvotes: 2

Views: 2976

Answers (2)

onmyway133
onmyway133

Reputation: 48205

It only works when Facebook app is installed. Also, specify mode = .native explicitly.

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()

If not, it will use SLComposeViewController, and you will get error

SLComposeViewController isAvailableForServiceType got serviceType com.apple.social.facebook isAvailable 0

Good practice is to debug on FBSDKShareDialog.m to see if things don't work for you

- (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;
}

Lesson learned:

  • Be aware of FB changes.
  • Check for change log every morning
  • Things that work today may not work tomorrow

Upvotes: 0

ThunderStruct
ThunderStruct

Reputation: 1504

I know this is an old question but just in case anyone faces the same issue:

According to the documentation, You need the native Facebook app to be installed in order to use FBSDKSharePhotoContent and you cannot use FBSDKShareDialog in that case.

A close workaround to achieve almost the same result is using FBSDKShareDialog (which only supports FBSDKShareLinkContent) as follows:

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()

You can find the protocol's methods here.

Upvotes: 2

Related Questions