In this post, we will look into integrating the Social Framework
for iOS
, so we can post to Twitter
and Facebook
from other applications we want to integrate with the Social Framework
. To start, let’s create a Single View Application
in Xcode
. In the storyboard let’s add a UITextView
connected to an IBOutlet
named socialTextView and a UIButton
on top of the text view, and connected to an IBAction
named shareAction.
Next, let’s write some code. In the view controller, inside the shareAction() method let’s create a UIAlertController
:
This will create a small pop-up window showing us an alert titled Share when we press the button. As you can see there is no way of getting rid of this alert, so let’s also add a button named Cancel which is a UIAlertAction
actually. Now we can dismiss the alert when we want to return back to the previous screen:
Then, import the Social framework so we can use its Twitter and Facebook components:
Next, back inside the shareAction() method, we will add a Twitter
action to our alert controller:
Let’s digest this code block. First, we created an action like we did for Cancel
but this time we wanted to do something in the completion handler so we created a block. Note that when you have a block as the last parameter of a function, it can be taken outside of the parentheses for convenience. Inside, we create a SLComposeViewController
which is a customized type of view controller provided by the Social
framework. When choosing the service type, we pick SLServiceTypeTwitter this time.
Next, we want to grab the text we typed in our text view (if any) and set it as out tweet message. However, keep in mind that Twitter
limits your messages to only 140
so we’re trimming our message if it’s longer than that. Finally, we need to also let the users know they are not logged in Twitter
if this is the first time they’re using Twitter
on this device/simulator. In order to log in to Twitter
they would need to go to the Settings
app and scroll down to Twitter
to log in. One more thing to do, add the Twitter
action to our action controller:
If you run the app, you will probably see a message that you are not logged in to Twitter
so go on and log in and then run the app again. This time you will see the Twitter
window ready to post your message. Be careful what you’re saying as this will get posted to your live Twitter
account. Next, let’s do the same for the Facebook
integration:
We don’t do anything different here, except we change the service type to SLServiceTypeFacebook. Run the app again and assuming you logged in to Facebook
already, you will see a similar type of pop-up window for posting to Facebook
. Again, be careful what you type as this will be posted to your live Facebook
account.
Finally, let’s also create a more general type of integration, so we are able to share our message with any other frameworks, not only with the Social
framework:
This time we create a UIActivityViewController
. If you run the app now, you will notice our message is not presented to us anymore, and we instead see the extensions view popping up from the bottom of the screen. This gives us even more sharing options for future integrations with other applications. The source code is available on Github.
Until next time!