× {{alert.msg}} Never ask again
Receive New Tutorials
GET IT FREE

Create a Facebook Share Button and Twitter Tweet Button for Your iOS Swift App

– {{showDate(postTime)}}

Hello, in this iOS Swift tutorial I will teach you how to share on Facebook and tweet on Twitter using the Social Framework.

To be able to share and tweet using Social Framework you must have the Facebook and Twitter Application installed on your iOS device.

##Building the App##

First create a new project in Xcode.
Select Single View Application and hit Next.

Name App SocialApp

enter image description here

Hit “Next” and “Create”.

In the main.storyboard we need to add 2 Buttons.

The first one with title: Share on Facebook

The second with title: Tweet on Twitter

enter image description here

Now we need to create 2 actions; one for each button.

##Creating a Facebook Share Button##

Open your assistant editor and Ctrl click - hold and drag from Facebook Button to the ViewController.swift file, and then release your click.

One the popup box select: Connection from outlet to action
and Name to facebookBtn

enter image description here

Hit “Connect” to create the action.
Close the assistant editor and navigate to your ViewController.swift file

First we need to import Social Framework

After you’ve imported the UIKit, add:
objectivec import Social

And you should have something that looks like this:

enter image description here

Now let’s write the code in the facebookBtn action.

Add the code:


if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
    var fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
    
    self.presentViewController(fbShare, animated: true, completion: nil)

} else {
    var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
    
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}

Where it will look like this:

enter image description here

###Understanding the Code###

First we have to check if the user has their Facebook Application installed and if they’re logged in.

We do this with this line of code:


if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
}

If the user has not installed the Facebook app or is not logged in, we create a UIAlertController and notify the user that they need to Login to Facebook to share.

We create the UIAlertController with this code:


var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
            
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            
self.presentViewController(alert, animated: true, completion: nil)

We create a variable called alert as our UIAlertController, with title “Accounts”, message “Please login to a Facebook account to share.” and the style UIAlertControllerStyle.Alert.

Next, we add an action to this alert using alert.addAction with the title “OK”, the style as “Default”, and the handler to “nil”.

Our last step is to present this Controller. We use self.presentViewController: alert animated true and completion: nil.

If the user has installed the Facebook app and is logged in, then write the code to open the “Share Controller”.

Our first step to do this is to create a variable fbShare as your SLComposeViewController for ServiceType: SLServiceTypeFacebook.

And the last step is to present this fbShare Controller using:
self.presentViewController: fbShare animated true and completion: nil.

###Running the App###

If you run the application in the simulator and press the “Share on Facebook” Button, you will see the AlertController because your simulator does not have the Facebook app.

However, if you Run the app on a real iOS device with Facebook installed, you will see the Facebook Share Controller.

enter image description here

Type something and hit Post, and you will see your post in your Facebook profile.

##Creating Your Tweet Button##

First we need to create an action for the other button.

With the same way as your Facebook Share button, open your Assistant Editor and Ctrl click, drag, and release under the shareBtn. Select “Action” as your Connection, and name it tweetBtn.

enter image description here

The code for it will look like this:

enter image description here

Now the code we need is the same as before, but with one change: we ask for SLServiceTypeTwitter .

Add this code:


if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) {
    
    var tweetShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
    
    self.presentViewController(tweetShare, animated: true, completion: nil)

} else {
    
    var alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to tweet.", preferredStyle: UIAlertControllerStyle.Alert)
    
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    
    self.presentViewController(alert, animated: true, completion: nil)
}

Where you should have something that will look like this:

enter image description here

As with the Facebook share button, we’ll check if the user has Twitter installed and if the user is logged in.

If not, we present the Alert messages, if yes, we open the Twitter Controller.

As with the Facebook sharing function, if you run the app in a real iOS device and press the “Tweet on Twitter” Button, you will see the Twitter Controller.

enter image description here

Type something and hit “post”, and you’ll have your Tweet posted.

The tutorial ends here - happy coding!



Author
Valsamis
Valsamis
5.0
IOS Developer, Instructor and Udemy Expert. I have 8 applications in App Store and 6 Courses
I'm an iOS developer that has been focused on mobile app design and creation for over 3 years. Since now i have 8 applications in the App Strore and 6 Udemy Courses! Since child i loved...
Hire the Author

Questions about this tutorial?  Get Live 1:1 help from Swift experts!
Miroslav Kuťák
Miroslav Kuťák
5.0
750+ sessions, #1 iOS & Swift Mentor in 2016, senior iOS dev, co-founder @Movez LLC
5+ years of professional iOS developer experience (iOS 4-10), 20+ AppStore apps, Czech Technical University graduate. Obj-C & Swift, strong in OOP...
Hire this Expert
Maxim Kholyavkin
Maxim Kholyavkin
5.0
iOS & OS X developer
I program iOS apps since the first iPhone. Also I have huge experience in C/C++ I worked in WW corporation - i can teach best coding...
Hire this Expert

Or Become a Codementor!

Live 1:1 help from expert developers

Codementor is your live 1:1 expert mentor helping you in real time.

comments powered by Disqus
Codementor is your live 1:1 expert helping you in real time