swift 中的 Facebook 本机共享对话框

Facebook Native Share Dialog in swift

我正在尝试创建将在 Facebook 上分享 link 的分享按钮。问题是我不想在我的应用程序中的分享按钮之前有一个登录按钮,我只需要一个分享按钮。

我认为一种解决方案可能是将自定义按钮用于登录和共享按钮,并将这些按钮合并为一个。这样,如果用户在单击共享按钮时尚未登录,他将被要求登录。然而,在我尝试实现这一点的过程中,我发现 Native Share Dialog 是更好的解决方案,因为它根本不请求登录。

但我很难找到任何 references/tutorial/examples 或与 swift 一起使用本机共享对话框的任何内容。 Obj-c 上有例子和东西,但我从来没有使用过 Obj-c,我对此一无所知。

如果有人有任何示例或者任何人都可以指出正确的方向如何在 Swift 中实现 Facebook 本地共享对话框,我很高兴。

如果没有,任何人都有在 Swift

上使用自定义共享按钮的任何示例

实际上您不需要自己处理登录,在 iOS 如果您使用 iOS SDK 的 UIActivityViewController,iOS 会为您处理。

来自文档:

声明 (SWIFT)

init(activityItems activityItems: [AnyObject], applicationActivities applicationActivities: [AnyObject]?) 

参数

activityItems The array of data objects on which to perform the activity. The type of objects in the array is variable and dependent on the data your application manages. For example, the data might consist of one or more string or image objects representing the currently selected content.

Instead of actual data objects, the objects in this array can be objects that adopt the UIActivityItemSource protocol, such as UIActivityItemProvider objects. Source and provider objects act as proxies for the corresponding data in situations where you do not want to provide that data until it is needed.

This array must not be nil and must contain at least one object. applicationActivities An array of UIActivity objects representing the custom services that your application supports. This parameter may be nil.

代码被截断:

let so:NSURL = NSURL(string:"http://whosebug.com")
let activityVC = UIActivityViewController(
            activityItems: ["Some text here...", so],
            applicationActivities: nil)
self.navigationController.presentViewController(activityVC, 
   animated: true, 
   completion: nil)

或者,您可以为此使用 Facebook 的 SDK(您需要添加到您的项目中)。 SDK 也将处理登录。

这是先决条件:

Before you can share to Facebook from your app, you'll need to:

Add the Facebook SDK for iOS to your mobile development environment Get a Facebook app ID, properly configured and linked to your iOS app Add your app id and display name to your app's .plist file. Link the FBSDKShareKit.framework to your project.

这里是 Facebook SDK 页面:

https://developers.facebook.com/docs/sharing/ios

这里是 URL 分享的一些代码片段:

let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: self.short_string)
content.contentTitle = self.title_text
// ... etc.

let button : FBSDKShareButton = FBSDKShareButton()
button.shareContent = content

其实我想出了一个解决办法

这些代码行将在 Swift 中实现 Facebook 本机共享对话框:

let content: FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: self.contentURL)
content.contentTitle = self.contentTitle
content.contentDescription = self.contentDescription
content.imageURL = NSURL(string: self.contentURLImage)
FBSDKShareDialog.showFromViewController(self, withContent: content, delegate: nil)