At WWDC 2012, which was held in June of this year, the Social framework was introduced among the innovations. This framework allows you to integrate sharing buttons on social networks quickly and easily. Social framework supplanted Twitter framework, introduced in iOS 5. Now we have the opportunity to integrate not only Twitter, but also Facebook and Weibo (the Chinese equivalent of Twitter) into our applications without unnecessary problems.
Let's start. The first thing you need to create a new project in Xcode, you can choose any name. Project Type - Single View Application.
To begin with, we will create an application interface, in our case it will consist of a picture window, a text entry form and two buttons.
In the window of the map, an image will be displayed that will be riveted to your post, in the input form you will be able to enter a link that you also want to share, and 2 buttons will be used to select the social network we need.
')
First of all, open ViewController.h and declare UIImageView and UITextField. We will need to make sure that our ViewController is a UITextField delegate. All this is done very simply, this is how the ViewController.h code should look like in your project.
#import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate>{ UITextField *textField; UIImage *shareImage; } @end
Now we can proceed to the most interesting - the creation of the application interface.
By the way, do not forget to add any image to the project - it will be attached to your posts in social networks.
Open ViewController.m and find the viewDidLoad method. It is called when the ViewController is loaded, so we will “add” interface elements to your application window there. Here is the code that is used to create the entire interface.
- (void)viewDidLoad { shareImage = [UIImage imageNamed:[NSString stringWithFormat:@"48X48.jpg"]]; UIImageView *imageView = [[UIImageView alloc]initWithImage:shareImage]; imageView.frame = CGRectMake(138, 40, 48, 48); [self.view addSubview:imageView]; CGRect textFieldFrame = CGRectMake(20.0f, 100.0f, 280.0f, 31.0f); textField = [[UITextField alloc] initWithFrame:textFieldFrame]; textField.placeholder = @""; textField.backgroundColor = [UIColor whiteColor]; textField.textColor = [UIColor blackColor]; textField.font = [UIFont systemFontOfSize:14.0f]; textField.borderStyle = UITextBorderStyleRoundedRect; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.returnKeyType = UIReturnKeyDone; textField.textAlignment = UITextAlignmentCenter; textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; textField.delegate = self; [self.view addSubview:textField]; UIButton *twitterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [twitterButton addTarget:self action:@selector(twiShare:) forControlEvents:UIControlEventTouchDown]; [twitterButton setTitle:@"Share on Twitter" forState:UIControlStateNormal]; twitterButton.frame = CGRectMake(80.0, 170.0, 160.0, 40.0); [self.view addSubview:twitterButton]; UIButton *facebookButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [facebookButton addTarget:self action:@selector(fbShare:) forControlEvents:UIControlEventTouchDown]; [facebookButton setTitle:@"Share on Facebook" forState:UIControlStateNormal]; facebookButton.frame = CGRectMake(80.0, 240.0, 160.0, 40.0); [self.view addSubview:facebookButton]; [super viewDidLoad];
Now you can compile your project, the interface should look like this:
Large interface screenshot If you try to press any of the buttons, the application will crash. This will happen because we have not yet added the twiShare and fbShare methods, which are called when the corresponding button is clicked. To fix this, you need to create these methods. This is done like this:
-(void)twiShare:(id)sender{ } -(void)fbShare:(id)sender{ }
Compile the project again, now the program should not fall when you click on the "social" buttons.
When you click on the text input window, the keyboard should appear, but when you click on the “Done” button, the keyboard will not close. To fix this, we need to add a method responsible for closing the keyboard. This is done like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; }
Try to copy the project again, now the keyboard closes when you click on the “Done” button.
The interface is ready, now let's get down to the most interesting part - setting up sharing on pressing one of the buttons.
The first step is to add Social.framework to your project. Open the Frameworks folder in the project, right-click on one of the added frameworks and select “Show in Finder”. Now the folder with all the frameworks should open, it remains for you to find and add Social.framework to the project.
Do not forget to import Social.framework into the ViewController, otherwise the application will produce compilation errors after adding sharing functions to the buttons. This is done like this:
#import "Social/Social.h"
Now we have to configure the sharing buttons in the application. Add this code to the twiShare and fbShare methods:
SLComposeViewController *composeController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; [composeController setInitialText:@" iOS 6"]; [composeController addImage:shareImage]; [composeController addURL: [NSURL URLWithString: [NSString stringWithFormat:@"%@",textField.text]]]; [self presentViewController:composeController animated:YES completion:nil];
.
Now when you click on the sharing button, a Twitter window will open. To click on the Share on Facebook button to open the Facebook window, you only need to change SLServiceTypeTwitter to SLServiceTypeFacebook. You can also change it to SLServiceTypeSinaWeibo, if you want to add the possibility of sharing in the Chinese equivalent of Twitter - Weibo.
Try to compile the project again, now everything should work. If you are not logged in to one of the social networks, then when you click on the corresponding button, you will enter the settings.
Large screenshot of the login offer Here’s what the standard sharing windows on Twitter and Facebook look like:
Large screening windows sharinga Download the working project
here .