使用 Swift 3 和 Swift SDK (0.2.0) 分享 Facebook

Facebook sharing with Swift 3 & Swift SDK (0.2.0)

我成功安装了 Facebook pods(所有三个),我可以在我的应用程序中登录用户,但我不能分享任何东西。

代码非常简单(来自示例项目,非真实项目):

import FacebookLogin
import FacebookShare

在viewDidLoad()中:

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

我还有:

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

关于这个,我不断收到此错误消息:

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

我没有关于 "try!" 的问题(我知道这是一种不好的做法),但我不明白我的代码有什么问题,以及如何理解这里的问题。

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

对于 Swift >= 5.0,FBSDKShareKit,使用共享对话框和消息对话框在 Facebook 和 Messenger 中共享您的媒体

详细教程可参考下方link

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