Ios: How to Share Text and Image on Social Networks

iOS: How to share text and image on social networks?

It seems that a recent update to the Facebook application has replaced the in-built Facebook share activity with one that ignores status text - If you remove the Facebook app from your device the text will be displayed using the code you have. If you have the Facebook app installed you get images, URLs but not the text

Facebook's policies don't allow you to pre-populate status messages and require all content to be user generated - while I understand the intention behind this, I personally think it is kind of stupid in many cases - For example in my game I want to pre-populate the user's score, but now I can't, so the user is presented with an empty dialog box. I will probably simply remove the Facebook sharing option as no-one will ever use it now.

This response from Facebook confirms that the behaviour is by design

iOS sharing picture on Social Networks

iOS has an inbuilt social sharing kit. You can share images via Email, Facebook and Twitter. But for using Google+ and other social services you will need their respective SDKs.

1) For Facebook

SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:message];
[controller addImage:image];
[self presentViewController:controller animated:YES completion:Nil];

2) For twitter replace SLServiceTypeFacebook with SLServiceTypeTwitter.

3) For Email

MFMailComposeViewController *emailShareController = [[MFMailComposeViewController alloc] init];
emailShareController.mailComposeDelegate = self;
[emailShareController setSubject:@"Share Image"];
[emailShareController setMessageBody:message isHTML:NO];
[emailShareController addAttachmentData:UIImageJPEGRepresentation(image, 1) mimeType:@"image/jpeg" fileName:@"your_image.jpeg"];
if (emailShareController) [self presentViewController:emailShareController animated:YES completion:nil];

4) Remember to add Social.Framework to your project and the following header files

#import 
#import
#import

5) Set your view controller as delegate of

MFMailComposeViewControllerDelegate

Dismiss MailViewController once mail is send-

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}

Sharing images on social media

I think that you should stick to UIActivityViewController to avoid eventual app rejection during the App Store publication Review process.
You could create a method for every button you have created that opens the UIActivityViewController only for the intended social network,
to do that you have to exclude all ActivityType except the one related to your button's social network:

@IBAction shareOnFacebook(_ sender: UIButton) 
{
let activityViewController = UIActivityViewController(activityItems: ["Your message"], applicationActivities: nil)
activityViewController.excludedActivityTypes = [
// Here: exclude all ActivityType except postToFacebook ...
]

present(activityViewController, animated: true)
}

For a complete list of available ActivityType see here

How to integrate social media for photo sharing on iOS?

Try ShareKit (https://github.com/ShareKit)

How to share data in social media app like as whatsapp, facebook using swift

I think there is no problem with your sendWhatsapp code. Did you add whatsapp into your Info.plist LSApplicationQueriesSchemes? You need to add for it to work.

To share on facebook and twitter. They are different

import Social 

if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
var fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
self.presentViewController(fbShare, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}

Swift 4.0:

if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) {
var fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
self.present(fbShare, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}

For twitter: just replace SLServiceTypeFacebook with SLServiceTypeTwitter.



Related Topics



Leave a reply



Submit