Show Menu

Looking to hire an app developer?

Submit your 30 day Job Listing for FREE

In this iPhone development tutorial we are going tο show how you can post on Facebook and Twitter via your app using the Social framework.

Let’s get started!

First of all you need to create a new Xcode Single View Application:

xcode single view app

Then, click Next and you will be prompted to another screen asking you the project settings. Type in the first field (“Product name”) the name of your project and click Next.

Importing the Social Framework

In order to use Social Framework you need to include the framework

  • Click on the Target
  • Click the “+” button on “Linked Frameworks and Libraries” section in order to access frameworks.
  • Finally click “Add”.
Import Social Framework
xcode choose framework

In order to use the Core Location framework you need to import it in the header file (in our case “ViewController.h”) as shown below:


#import "Social/Social.h"
@interface ViewController : UIViewController

In our header file we should also declare some variables as shown above and two actions.


@interface ViewController : UIViewController
{
 IBOutlet UIButton *Tweet;
 IBOutlet UIButton *FBPost;
}
-(IBAction)TweetPressed;
-(IBAction)FBPressed;
@end

Designing the screen

Now click on the “Main.storyboard” file and drag 2 UIButtons on the view. When you arrange the 2 UIButtons , link each UIButton and each IBAction declared with the correspondent UIButton on the view as shown in the picture below. For the IBActions select the “Touch Up Inside” event.

xcode link
xcode link

Let’s get serious

It is time to edit the implementation file (in our case “ViewController.m”) .
Add the following code in the ViewController.m file in order to be able to post on facebook by tapping the “Post on Facebook” button.


-(IBAction)FBPressed{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *fbPostSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [fbPostSheet setInitialText:@"This is a Facebook post!"];
        [self presentViewController:fbPostSheet animated:YES completion:nil];
    } else
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Sorry"
                                  message:@"You can't post right now, make sure your device has an internet connection and you have at least one facebook account setup"
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

In the second line we check if facebook is available on the iOS device that our app runs.
Then we instantiate the SLComposeViewController and we set service type to SLServiceTypeFacebook. Then, we present the compose view controller. If facebook is not available on the device on which our apps run, then we present a UIAlert informing the user that he has not set any facebook account on his device.

And for twitter:


-(IBAction)TweetPressed{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"This is a tweet!"];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Sorry"
                                  message:@"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }

}

As you can see implementation for twitter is pretty much the same with the implementation for facebook.

Let’s run it!

Now run the application in the simulator. Try to press the buttons to tweet and post on facebook from the app that you created.

Post on facebook
Post on facebook

Final Notes

I hope you find this tutorial helpful. If you have any question do not hesitate to ping me on Twitter or leave a comment below.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


Written by:

I am 17 years old, student and amateur iOS app developer, computer and technology enthusiast. Check out my apps using the link provided and also use the other link to follow me on twitter

Comments

comments