Apple makes it incredibly easy to add social sharing to your app. There are at least two ways to do this.
The first calls on the UIActivityViewController which has some built-in, behind the scenes magic. It is extremely simple and does not use a Framework.
UIActivityViewController will bring up any available sharing mechanism on the users phone. For example when the share button is touched, a view slides up with the available places to share. Mail, Messaging, Print, Photo Album etc. If the user has the Twitter or Facebook app on their phone, those icons will be presented as well.
The second way is using the Framework Social The Social Framework allows you to target specific social outlets supported by the framework.
In Xcode 6 the available outlets are:
SLServiceTypeFacebook;
SLServiceTypeTwitter;
SLServiceTypeSinaWeibo;
SLServiceTypeLinkedIn;
SLServiceTypeTencentWeibo;
Implementing the code is quite simple and a powerful way to connect your app to these supported social outlets.
Lets get started.
Create a new project, single view application, name it ShareDemo. For the Language chose Objective-c, and target device, iPhone.
In Image.xcassets add an image and name it what every you like. Ill name mine boyOnBeach.
Open the Storyboard. Drag on a button and name it “Share”.
Open the assistant editor and make the connections.
Control+Drag from the button to the ViewController.h file just under @interface. Make sure action” is selected in the pop up window and name it shareButton.
#import
@interface ViewController : UIViewController
- (IBAction)shareButton:(id)sender;
@end
Move on to the implementation file. Anywhere in the code create a method which we will call when the share button is touched. I will name this method shareContent. Our method will contain a text string and an image. Well put those into an array and call on the ActivityViewController to take over. Below is Apples description of the ActivityViewController.
“The UIActivityViewController class is a standard view controller that you can use to offer various services from your application. The system provides several standard services, such as copying items to the pasteboard, posting content to social media sites, sending items via email or SMS, and more. Apps can also define custom services.”
Our code should look something like this.
-(void)shareContent{
NSString * message = @"My too cool Son";
UIImage * image = [UIImage imageNamed:@"boyOnBeach"];
NSArray * shareItems = @[message, image];
UIActivityViewController * avc = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];
[self presentViewController:avc animated:YES completion:nil];
}
Build and Run. I have found this doesn’t always work in the simulator. I recommend testing on a device.
You should see something like this below.
This is a “bare bones” but powerful way to share via the resources available on the users device.
Next well look at how to target specific social sharing outlets using the Social Framework.
Download the full project here.
Zhou Hao says
The image and text is not shown in email at all in iOS 8. Do you know what’s wrong with that? (My application using UIActivityViewController has the same problem.)
Martin says
This appears to be an Apple(Xcode) issue as it crashes when using the simulator, but works fine on a real device.
masse says
Nice tutorial!!
Jolin says
when i test on it, it showed me that the facebook plugin invalidate. it not share at FB page at all
jak says
When i tested in iOS 9-Sharing via mail in UIActivity View Controller not Working.I got error like MailCompositionService quit unexpectedly.
Sreedhar says
Its working fine with email , message but while sharing through Facebook ,image is not appearing