Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 308
lithium Avatar asked Dec 14 '25 18:12

lithium


1 Answers

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)
}
like image 199
Alok Nair Avatar answered Dec 17 '25 10:12

Alok Nair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!