0

I will try to be clear in my problem. I want to enable facebook sharing in my swift app (Swift 3). When the user clicks the link on facebook, he should be redirected on the app (if he has it) on app store (if he is on iOS and doesn't have app) or another website for other case.

Facebook provides a simple tool to do that : App Links : https://developers.facebook.com/docs/applinks

So then I just had to create on my ios project a FBSDKShareLinkContent class and give it the appLink url, a title, a description and an image :

let content:FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: linkURL)
content.contentTitle = nsTitle as! String
content.contentDescription = nsDesc as! String
content.imageURL = NSURL(string: imageURL)

But facebook change it's way to share content. The properties contentTitle, contentDescription and imageURL are now read only and deprecated :

/**
      The URL of a picture to attach to this content.
     - Returns: The network URL of an image

     @deprecated `imageURL` is deprecated from Graph API 2.9.
     For more information, see https://developers.facebook.com/docs/apps/changelog#v2_9_deprecations
     */
    @available(*, deprecated, message: "`imageURL` is deprecated from Graph API 2.9")
    open var imageURL: URL! { get }

Now facebook uses metas from the contentURL website to show image, title and description. The problem is that the url given by facebook app link soen not provite any title, description or image data, here is the http reponse :

<html>
    <head>
        <title>Mowwgli</title>
        <meta property="fb:app_id" content="1786124768326289" />
        <meta property="al:ios:url" content="mowwgli://" />
        <meta property="al:ios:app_name" content="Mowwgli" />
        <meta property="al:ios:app_store_id" content="1184953498" />
        <meta property="al:web:should_fallback" content="false" />
        <meta property="al:web:url" content="http://www.mowwgli.com/" />
        <meta http-equiv="refresh" content="0;url=http://www.mowwgli.com/" />
    </head>
</html>

Then the sharing does not show any image title or description :

rendering of facebook share

Houman
  • 64,245
  • 87
  • 278
  • 460
brice cesarin
  • 105
  • 2
  • 7

1 Answers1

0

I found myself the solution using this post : How to implement iOS app link Facebook functionality in swift 3?

His problem was that he succeeded to share image, title and description but no app link. So I used what he did and used FBSDKShareOpenGraphContent instead of FBSDKShareLinkContent. The sharing dialog can use both ones. Then my code is now :

  let properties = [
        "og:title": nsTitle,
        "og:description": nDesc,
        "og:image" : imageURL,
        "og:url" : linkURL,
    ]

    let object : FBSDKShareOpenGraphObject = FBSDKShareOpenGraphObject.init(properties: properties)

    // Create an action
    let action : FBSDKShareOpenGraphAction = FBSDKShareOpenGraphAction()
    action.actionType = "news.publishes"
    action.setObject(object, forKey: "article")

    // Create the content
    let content : FBSDKShareOpenGraphContent = FBSDKShareOpenGraphContent()
    content.action = action
    content.previewPropertyName = "article"

    FBSDKShareDialog.show(from: vc, with: content, delegate: nil)
brice cesarin
  • 105
  • 2
  • 7
  • I had tried same way, but not succeed. I am getting this error "The operation couldn’t be completed. (Com. Facebook. SDK. share error 2.)". I am using the simulator. any idea about that? – Harshadcse Jul 20 '17 at 17:31
  • According to below url : https://developers.facebook.com/docs/sharing/opengraph/ Custom Open Graph stories are deprecated in Graph API 2.8. Graph API 2.7 and lower will support custom Open Graph stories according to the following schedule: Support for creating new objects will end in January 2017. Support for publishing existing objects will end October 2017. This means that this would be a temporary solution till October. Is there an alternative way to do provide an external link and post with Photo ? – Harshadcse Jul 22 '17 at 16:01
  • Only custom open graph stories are deprecated, not the standard ones. So we can use FBSDKShareOpenGraphContent safely. However, I can't seem to make it work using news actions. books actions work fine but the book has to have an isbn field which I can't provide. The only action left for me is "og.likes"... – Bioche Jul 30 '17 at 13:29
  • https://stackoverflow.com/questions/9966859/android-facebook-open-graph – Jeff Bootsholz Jun 04 '19 at 14:38