Facebook and Twitter Sharing in IOS

Create a new Project in xcode and add Social.framework into your project. Good technical learning during corona times as well.

Check Whether you have signed in facebook and twitter account in your iphone settings.If not sign in.

In ViewController.h file,
#import
@interface ViewController : UIViewController
- (IBAction)shareButton:(id)sender;
@end

In ViewController.m file,
#import "ViewController.h"
#import
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{ [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)shareButton:(id)sender {
//This is how you create a UIACtionSheet.
UIActionSheet *share = [[UIActionSheet alloc] initWithTitle:@"Share via" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Twitter", @"Facebook", nil];
//You must show the action sheet for the user to see it.
[share showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Each button title we gave to our action sheet is given a tag starting with 0.
if (buttonIndex == 0) {
//Check Twitter accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
//This is setting the initial text for our share card.
[tweetSheet setInitialText:@"This is for Initial Text"];
[tweetSheet addURL:[NSURL URLWithString:@"http://sanjeevios.blogspot.in/"]];
[tweetSheet addImage:[UIImage imageNamed:@"Argentina.jpg"]];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:tweetSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"You can't send a tweet right now, make sure you have at least one Twitter account setup and your device is using iOS6." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}else if (buttonIndex == 1) {
//Check Facebook accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebookSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
//This is setting the initial text for our share card.
//[facebookSheet setInitialText:@"veasoftware.com made it easy to intergate Facebook with iOS 6! :D "];
[facebookSheet setInitialText:@"This is for Initial Text"];
[facebookSheet addURL:[NSURL URLWithString:@"http://sanjeevios.blogspot.in/"]];
[facebookSheet addImage:[UIImage imageNamed:@"Argentina.jpg"]];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:facebookSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"You can't post a Facebook post right now, make sure you have at least one Facebook account setup and your device is using iOS6." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
}
@end

Leave a Reply