Facebook and Twitter Sharing in iOS application using Social Framework

Facebook and Twitter Sharing in iOS

If you are working on iOS app development, then these days, everyone prefers to implement the social network sharing features like Twitter and Facebook sharing. If implemented, then this will add value to your iOS app and boost its popularity with Facebook and Twitter sharing support.

Let’s start to learn how you can integrate Facebook and Twitter sharing using Social framework.

Create class named SocialSharingVC with XIB named SocialSharingView Add two buttons in SocialSharingView named btnFacebookSharing and btnTwitterSharing and also define action events for both buttons btnFacebookSharing_Clicked and btnTwitterSharing_Clicked.

The social framework is not included when you create the new xCode project. So add Social.framework in the project.
Below is the final code for the SocialSharingVC.h class.

#import <UIKit/UIKit.h>

@interface SocialSharingVC : UIViewController

- (IBAction)btnFacebookSharing_Clicked:(id)sender;
- (IBAction)btnTwitterSharing_Clicked:(id)sender;

@end

Now all the code that you have to write in your implementation class SocialSharingVC.m for facebook and Twitter sharing is as given below.

First import the Social.h file at very top of the class SocialSharingVC.m as we have to use SLComposeViewController which is a class provided by social framework.


#import <Social/Social.h>

(IBAction)btnTwitterSharing_Clicked:(id)sender {
 if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
 {
 SLComposeViewController *tweetSheetOBJ = [SLComposeViewController
 composeViewControllerForServiceType:SLServiceTypeTwitter];
 [tweetSheetOBJ setInitialText:@"Learn iOS programming at mycodetips.com"];
 [self presentViewController:tweetSheetOBJ animated:YES completion:nil];
 }
}

(IBAction)btnFacebookSharing_Clicked:(id)sender {
 if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
 SLComposeViewController *fbSheetOBJ = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
 
 [fbSheetOBJ setInitialText:@"First post from my iOS application in mycodetips"];
 [self presentViewController:fbSheetOBJ animated:YES completion:Nil];
 }
}

(IBAction)btnFacebookSharing_Clicked:(id)sender {
 if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
 SLComposeViewController * fbSheetOBJ = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
 
 [fbSheetOBJ setInitialText:@"Post from my iOS application"];
 [fbSheetOBJ addURL:[NSURL URLWithString:@"http://mycodetips.com"]]; 
 [fbSheetOBJ addImage:[UIImage imageNamed:@"my_image_to_share.png"]]; 
 [self presentViewController:fbSheetOBJ animated:YES completion:Nil]; } }

Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top