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:
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”.
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:
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.
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.
Final Notes
I hope you find this tutorial helpful. If you have any question do not hesitate to ping me onTwitteror leave a comment below.
No comments:
Post a Comment