Facebook与Swift 3& Swift SDK(0.2.0)

时间:2016-12-01 18:40:00

标签: ios swift facebook facebook-ios-sdk

我成功安装了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

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

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

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

您可以点击以下链接了解详细教程。

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