0
votes

I am working on sharing a content on Facebook for an iOS app using Swift.

I have written a singleton class called FBManager and a function as below.

func shareContent(content:String, contentURL:String?, contentTitle:String? , fromController controller:UIViewController {
        let shareDialog = FBSDKShareDialog()
        let shareLinkContent = FBSDKShareLinkContent()

        shareLinkContent.contentDescription = content

        if let url = contentURL
        {
            shareLinkContent.contentURL = NSURL(string: url)
        }

        if let title = contentTitle
        {
            shareLinkContent.contentTitle = title
        }

        shareDialog.delegate = self
        shareDialog.fromViewController = controller
        shareDialog.shareContent = shareLinkContent
        shareDialog.show()
  }

But this does not even show a share dialog both on iOS 8 and iOS 9. Instead the following delegate method gets called

func sharer(sharer: FBSDKSharing!, didFailWithError error: NSError!) {
}

with the error - "The operation couldn’t be completed. (com.facebook.sdk.share error 2.)"

Can someone please help ?

1

1 Answers

1
votes

Facebook SDK's error codes are somewhat ambiguous because they cover rather large domains of errors. The code you provided does not really show the content of the variables and so I cannot pinpoint the problem. However, com.facebook.sdk.share error 2 is an Invalid Argument error, which usually arises from an invalid format of one or more members of FBSDKShareLinkContent.

Generally, you can use the FBSDKErrorCode enum to switch over the (error as NSError).code and find which domain it belongs to. (In this case, it'll point to Invalid Argument)

You can also print(error) directly in the didFailWithError delegate method, which will output a very descriptive log of the error and what caused it specifically.

Check your contentURL, make sure it starts with http:// or https:// or any other valid protocol. Same for the imageURL if you're using or planning to use one. This most likely caused your error!

The SDK's error codes reference may be helpful too.