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;
}
}
Related
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];
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?
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];
Part of my iOS app is a file viewer with a UIActivityViewController. From it, I would like users to be able to do normal activity view stuff with the file, like mailing it, saving it to photos (if it's a photo), and everything else. However, my app has a bookmarks feature. I would like for users to be able to bookmark files from this menu. I have created a custom UIActivity to add the object to the bookmarks list, but I have not been able to figure out how to have it use the system bookmark icon. Is this even possible?
EDIT: For clarification, this is in the menu that you get when you click a "share" button.
Easy peasy. It all depends on how you are initiating it, is it in a navigation controller, swipe gesture, toolbar item or just a regular button? Either way it doesn't matter but if you want a specific answer you'll have to ask a specific question.
Here is an example how to accomplish your goal buy using a bookmark button that is connected in Storyboard interface, however the IBActions for addToFav and download aren't an interface and don't need to be:
-(IBAction) moreActions:(id)sender {
UIActionSheet *moreActions = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Add To Favorites", #"Download For Offline View",#"Open Document In :", nil];
[moreActions showInView:self.view];
}
And the way you perform actions is by calling the method below:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0: [self addToFav:self]; //See IBAction below
break;
case 1: [self download:self]; //See IBAction below
break;
case 2: {
//UIDocumentInteractionController:
NSURL *docURL = [NSURL URLWithString:#"filePathStringHere"];
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:docURL];
[documentInteractionController setDelegate:self];
[documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
break;
}
}
- (IBAction)download:(id)sender {
//enter your download code here
}
- (IBAction)addToFav:(id)sender {
//Code to add to your favorites
}
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.