This will build off social sharing using UIActivityViewController. If you have not viewed that tutorial you can jump to it here. Or download the project file from where the last tutorial ended. ShareDemo.
The steps to implement intentional sharing to one service are as follows:
– Add the Social Framework to the project
– Import the framework into the header file
– Add buttons for the social types we want to post to.
– Create a method to invoke sharing to the specified service type.
Firstly add the framework. Select your project, build phases. Select “link binary with libraries”, and click the + button on the lower left of it’s window. Search for “social” and the framework appears, then click “Add”.
In the ViewController .h file import the framework.
#import
#import
@interface ViewController : UIViewController
- (IBAction)shareButton:(id)sender;
@end
In Xcode 6 the available service types are:
SLServiceTypeFacebook;
SLServiceTypeTwitter;
SLServiceTypeSinaWeibo;
SLServiceTypeLinkedIn;
SLServiceTypeTencentWeibo;
In the storyboard add three buttons and name them “Facebook”, “Twitter”, and “Sina”. While Linkedin is a service type it is not available for iOS.
Make the connections to the .h file being sure to select ACTION and not OUTLET.
Your ViewController .h file should look like this…
#import
#import
@interface ViewController : UIViewController
- (IBAction)shareButton:(id)sender;
- (IBAction)FacebookShare:(id)sender;
- (IBAction)TwitterShare:(id)sender;
- (IBAction)SinaShare:(id)sender;
@end
Switching to the implentation file, create a method called “targetedShare” with “serviceType”.
-(void)targetedShare:(NSString *)serviceType{
}
Let’s add some code between our curly braces and explain a bit.
-(void)targetedShare:(NSString *)serviceType {
if([SLComposeViewController isAvailableForServiceType:serviceType]){
SLComposeViewController *shareView = [SLComposeViewController composeViewControllerForServiceType:serviceType];
}
The first thing we do is call on the SLComposeViewController. What is that you ask? Apple Definition: “The SLComposeViewController class presents a view to the user to compose a post for supported social networking services.”
And we ask if it is available for the selected service type. Lets look at the apple documentation here because it’s very important.
If you hold down the option key and hover over isAvailableForServiceType this is what you get.
If a service type is available we will create a compose view controller for our social connection.
Again, option clicking for more information about “composeViewControllerForServiceType” we get this…
Wrapping up our method we’ll add the body of what we’re sharing:
-(void)targetedShare:(NSString *)serviceType {
if([SLComposeViewController isAvailableForServiceType:serviceType]){
SLComposeViewController *shareView = [SLComposeViewController composeViewControllerForServiceType:serviceType];
[shareView setInitialText:@"My too cool Son"];
[shareView addImage:[UIImage imageNamed:@"boyOnBeach"]];
[self presentViewController:shareView animated:YES completion:nil];
Now just add this method to each button with the appropriate service type.
For example in our Facebook button:
- (IBAction)FacebookShare:(id)sender {
[self targetedShare:SLServiceTypeFacebook];
}
Repeat for the other buttons.
Build and Run.
If you touch here and it does not have an account the app does nothing. Add an alert view to handle the service type being unavailable.
-(void)targetedShare:(NSString *)serviceType {
if([SLComposeViewController isAvailableForServiceType:serviceType]){
SLComposeViewController *shareView = [SLComposeViewController composeViewControllerForServiceType:serviceType];
[shareView setInitialText:@"My too cool Son"];
[shareView addImage:[UIImage imageNamed:@"boyOnBeach"]];
[self presentViewController:shareView animated:YES completion:nil];
} else {
UIAlertView *alert;
alert = [[UIAlertView alloc]
initWithTitle:@"You do not have this service"
message:nil
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
Build and Run again.
Now if a button is touched and the user does not have one of the selected services on their phone an alert will appear.
Social Sharing has been made very easy by Apple. No more API’s for Facebook or Twitter. The Social Framework gives you a lot of functionality for “free”.
You can download the complete project here. ShareDemo-2.
CLAUDIO JOSE says
All that code works but if I put this app on a real device, the setinitialtext does not work appearing empty in the facebook post.
do you have a sollution ?
see you soon