简体   繁体   中英

Facebook Native Share Dialog in swift

I am trying to create share button which will share a link on Facebook. The problem is that I do not want to have a login button before share button in my app, I just need to have a share button.

I think one solution may be to use custom buttons for login and share buttons and merge these buttons in one. In that way if the user has not log in when he click share button he will be asked to login. However during the time that I was trying to achieve this I found that Native Share Dialog is even better solution because it dose not request to login at all.

But I am struggling to find any references/tutorial/examples or anything where Native Share Dialog is used with swift. There are examples and stuffs on Obj-c but I have never used Obj-c and I do not know anything about it.

I was hopping if anyone has any examples or can anyone point me in the right direction how to achieve Facebook Native Share Dialog in Swift.

If not dose anyone has any example of using custom share buttons on Swift

Actually I figure out a solution.

These lines of code would implement Facebook Native Share Dialog in Swift:

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)

You don't need to handle login by yourself actually, in iOS if you're using iOS SDK's UIActivityViewController , iOS will handle for you.

from documentation :

Declaration (SWIFT)

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

Parameters

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.

a code snipped :

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

Alternatively you can use Facebook's SDK for this (you need to add in your project). Also SDK will handle login.

Here is prerequisites :

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.

here is Facebook SDK page :

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

here is some code snippet for URL sharing :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM