lithium
lithium

Reputation: 1302

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.

Upvotes: 3

Views: 2545

Answers (2)

emraz
emraz

Reputation: 1613

For Swift >= 5.0, FBSDKShareKit, Share your media in Facebook and Messenger using Share Dialog and Message Dialog

You can follow the link below for details tutorial.

Facebook, Messenger - Photo, Video, Link sharing in iOS Swift.

Upvotes: 0

Alok Nair
Alok Nair

Reputation: 4004

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

Upvotes: 4

Related Questions