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

Please, can you tell me if I'm doing mistakes?
NSString *sharedMsg=[NSString stringWithFormat:#"Hello world"];
UIImage* sharedImg=[UIImage imageNamed:#"image"];
NSArray* sharedObjects=[NSArray arrayWithObjects:sharedMsg, sharedImg, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:sharedObjects applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];
At runtime when I select the icon of Facebook the text is missing, the image is correctly displayed.
My xcode is 6.3.1, tested on iPad.

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

Here is the answer in Swift. It may not help your problem, but it might be helpful with someone looking for this title. Just create a button and it's action. And install the button's action like this.
Note: You have to log in your facebook or twitter by going to setting. Then do like this.
#IBAction func onShareTouched(sender: AnyObject) {
print("share")
let myShare = "My beautiful photo! <3 <3"
let image: UIImage = UIImage(named: "yourImageNameHere")
let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [(image), myShare], applicationActivities: nil)
self.presentViewController(shareVC, animated: true, completion: nil)
}

NSString* text=#"Hello world";
NSURL *myWebsite = [NSURL URLWithString:#"http://www.website.com/"];
// UIImage * myImage =[UIImage imageNamed:#"myImage.png"];
NSArray* sharedObjects=#[text,myWebsite];
UIActivityViewController * activityViewController=[[UIActivityViewController alloc]initWithActivityItems:sharedObjects applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];

Related

UIActivityViewController not Sharing both Text and Image on Facebook [duplicate]

Please, can you tell me if I'm doing mistakes?
NSString *sharedMsg=[NSString stringWithFormat:#"Hello world"];
UIImage* sharedImg=[UIImage imageNamed:#"image"];
NSArray* sharedObjects=[NSArray arrayWithObjects:sharedMsg, sharedImg, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:sharedObjects applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];
At runtime when I select the icon of Facebook the text is missing, the image is correctly displayed.
My xcode is 6.3.1, tested on iPad.
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
Here is the answer in Swift. It may not help your problem, but it might be helpful with someone looking for this title. Just create a button and it's action. And install the button's action like this.
Note: You have to log in your facebook or twitter by going to setting. Then do like this.
#IBAction func onShareTouched(sender: AnyObject) {
print("share")
let myShare = "My beautiful photo! <3 <3"
let image: UIImage = UIImage(named: "yourImageNameHere")
let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [(image), myShare], applicationActivities: nil)
self.presentViewController(shareVC, animated: true, completion: nil)
}
NSString* text=#"Hello world";
NSURL *myWebsite = [NSURL URLWithString:#"http://www.website.com/"];
// UIImage * myImage =[UIImage imageNamed:#"myImage.png"];
NSArray* sharedObjects=#[text,myWebsite];
UIActivityViewController * activityViewController=[[UIActivityViewController alloc]initWithActivityItems:sharedObjects applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];

UIActivityViewController share image and text prevents Instagram activity

The following code brings up the activity view controller for an app that can share both images and text. Instagram only shows up if I remove the shareText item.
UIImage *shareImage = [UIImage imageNamed:#"image.jpg"];
NSString *shareText = #"some text";
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:#[shareImage,shareText] applicationActivities:nil];
I would like to be able to share to all services and just drop text if a specific service doesn't support it. I therefore implemented a UIActivityItemSource and attempted to filter based on activityType passed in to:
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(nullable UIActivityType)activityType
But activityType is almost always nil. So I can't filter on it.
Anyone have a solution?

iOS share image AND text to WhatsApp

I have googled, and get some solutions, it seems the only possible way is thru UIDocumentInteractionController. I have found the result that able to share text ONLY, also found result that share image ONLY.
But what I want is share BOTH.
I know this question may be duplicated, I just wanted to make it clear, here is the screenshot...
(This is shared from Android)
You can use UIActivityViewController to share image , text or URL .Here is a small example :
NSString *textToShare = #"Enter your text to be shared";
UIImage * image = [UIImage imageNamed:#"imagename"];
NSArray *objectsToShare = #[textToShare, image];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
Run the above code and select whats app to share if you want you can share by other mediums also . This is apple'
s default share method
something not usually mentioned the user doesn't actually needs to share a text message and an image.
If your text contains URL then the whatsapp application will try to retrieve info about the URL and show a preview
In order for this to work you need to make the URL conform to open graph protocol. that basically means that the URL needs to have meta tags in its DOM which contain the relevant preview data
Good one,
As I know it is not possible in ios.
But I am having an alternate solution for it by which you can share text and image both.But it's a tricky or I think stupid solution.
Create a View where you can put your image.Write text on that view whatever you want to write.
Take Screen shot of that view with help of code.You will get image (image of view where text and image added).
Just share that image via document interaction controller.
This is just a possible solution if you want text and image both.But if you want to share link with text than . . . . . . .
You can use UIDocumentInteractionController for this purpose like this:
#property (retain) UIDocumentInteractionController * documentInteractionController;
if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:#"whatsapp://app"]]){
UIImage * iconImage = [UIImage imageNamed:#"YOUR IMAGE"];
NSString * savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wai"];
[UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = #"net.whatsapp.image";
_documentInteractionController.delegate = self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
} else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"WhatsApp not installed." message:#"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
Check this answer for reference: https://stackoverflow.com/a/20601051/2082569
Also you can have a look at Socialize SDK that is also very easy to use and integrates with various social SDKs. Check this documentation for Whatsapp sharing: http://socialize.github.io/socialize-sdk-ios/whatsapp.html
Please check below project on github
https://github.com/salesawagner/SharingWhatsApp
typedef enum{
kSendText = 0,
kSendImage,
kSendTextWithImage,
kSendAudio,
kSendCancel
} options;
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case kSendText:
[[WASWhatsAppUtil getInstance] sendText:#"Text"];
break;
case kSendImage:
[[WASWhatsAppUtil getInstance] sendImage:[UIImage imageNamed:#"image.jpg"] inView:self.view];
break;
case kSendTextWithImage:
NSLog(#"Send text with image");
case kSendAudio:
[[WASWhatsAppUtil getInstance] sendAudioinView:self.view];
break;
default:
NSLog(#"Cancel send");
break;
}
}

Simple way to add Facebook/Twitter share to app?

I'm looking for a drop in solution to add a Facebook and Twitter share button option to my app. I've googled this of course, and found shareKit but it's non-ARC so just throws lots of errors, which is of no use to me. Any other options? they need to be ARC compatible.
I want a SDK that I can drop in, throw in the App ID's and be done.
If you want simple sharing, the following can be used for Twitter and Facebook:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[slComposeViewController setInitialText:#"Your text"];
[slComposeViewController addImage:[UIImage imageNamed:#"NameOfImage"]];
[self presentViewController:slComposeViewController animated:YES completion:nil];
} else {
//Show alert or in some way handle the fact that the device does not support this feature
}
For Facebook, simply replace SLServiceTypeTwitter with SLServiceTypeFacebook. The "Social" framework must be imported :-)
You can use UIActivityViewController:
NSArray * activityItems = activityItems = #[#"title", [NSURL URLWithString:#"urlstring"]];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
References : https://developer.apple.com/library/ios/documentation/uikit/reference/UIActivityViewController_Class/Reference/Reference.html
Yes use the native iOS sharing dialog: UIActivityViewController

standard way to implement share as function

I need to implement a "share as..." function in iOS.
For example a button names "share as..." and popup a dialog which includes items like Email, SMS, Facebook, Twitter.
I wonder if there have a standard dialog do this job.
After searching, I found a way seems quite "standard" way in iOS6 by using UIActivityViewController.
Following is the description from developer.apple.com:
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.
And following is a dialog I managed to produce by using UIActivityViewController
And following is the source code I use:
NSArray *activityItems = [NSArray arrayWithObjects: share_text.text, share_image.image , nil];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
In your ViewController.m :
NSString *message = #"The Flyer http://flyerdream.tumblr.com";
UIImage *image = [UIImage imageNamed:#"flyer"];
NSArray *arrayOfActivityItems = [NSArray arrayWithObjects:message, image, nil];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:arrayOfActivityItems applicationActivities:nil];
[self.navigationController presentViewController:activityVC animated:YES completion:nil];
After using UIActivityViewController, I believe it is a much better solution than what I've posted below. I've left my answer because the individual sharing methods may be of use to someone.
This is how I've implemented share features in my projects:
Email: Use MFMailComposeViewController. See documentation here: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html
Facebook & Twitter: Use SLComposeViewController. See documentation here: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html
Example:
// prepare and present SLComposeViewController if service is available
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *socialPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[socialPost setInitialText:#"TEXT FOR POST"];
[socialPost addImage:[UIImage imageNamed:#"yourImage.png"]];
[self presentViewController:socialPost animated:YES completion:nil];
}
// set up a completion handler
[socialPost setCompletionHandler:^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultDone:
// post was completed
break;
case SLComposeViewControllerResultCancelled:
// post was cancelled
break;
default:
break;
}
}
Note: If you want to post for Twitter, replace SLServiceTypeFacebook with SLServiceTypeTwitter in the code sample above.

Resources