iosswiftfacebookfacebook-ios-sdk

Facebook sharing with Swift 3 & Swift SDK (0.2.0)


I successfully installed Facebook pods (all three of them), and I can sign-in user in my app, but I can't share anything.

Code is very simple (it's from the sample project, not real one):

import FacebookLogin
import FacebookShare

In viewDidLoad():

button.addTarget(self, action: "sharePic", for: .touchUpInside)

And also I have:

func sharePic() {

    let url = URL(fileURLWithPath: "https://developers.facebook.com")        
    let content = LinkShareContent(url: url, title: "facebook", description: "facebook developers", quote: "fbd", imageURL: nil)
    try! ShareDialog.show(from: self, content: content)
}

On this I'm constantly getting this error message:

fatal error: 'try!' expression unexpectedly raised an error: FacebookShare.ShareError.reserved: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.58.6/src/swift/stdlib/public/core/ErrorType.swift, line 178

I don't have a question about "try!" (I know it's a bad practice), but I don't understand what is wrong with my code, and how to understand what is wrong here.


Solution

  • func showShareDialog<C: ContentProtocol>(_ content: C, mode: ShareDialogMode = .automatic) {
        let dialog = ShareDialog(content: content)
        dialog.presentingViewController = self
        dialog.mode = mode
    
        do {
            try dialog.show()
        } catch (let error) {
            Toast(text: "Invalid share content. Failed to present share dialog with error \(error)", duration: Delay.short).show()
        }
    }
    
    func fbClick() {
        var content = LinkShareContent(url: URL(string: "http://example.com")!,
                                       title: "Title",
                                       description: "Description",
                                       imageURL: "http://test.com/test.png")
    
        showShareDialog(self.content!, mode: .native)
    }