UIActivityViewController for sending an sms with image, url and a text - ios

I am using UIActivityViewController for sending an sms with image, URL and a text. But SMS is showing full url including all parameters, instead of a clickable short link.
NSURL *urlImage = [NSURL URLWithString:[NSString stringWithFormat:#"%#",imageUrl]];
NSData *data = [NSData dataWithContentsOfURL:urlImage];
UIImage *img = [[UIImage alloc] initWithData:data];
NSURL *url = [NSURL URLWithString:shareURl];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:#[#"I just won this on test. Check it out!", url, img] applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeSaveToCameraRoll, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter];
[self presentViewController:activityVC animated:YES completion:nil];
What I am getting is:
+
What I need is:
Is there any way we can achieve this?

Related

objective-c share image to Instagram from my iOS application

I've spent a long time searching how to share photo from iOS application to Instagram. Other social media like Twitter are working fine. I have found a lot of resources on Google but it's not working.
Please I need some help to add Instagram share button to the UIActivityViewController that already has Twitter share button.
Here is my code:
- (IBAction)shareBtnClicked {
NSString *textToShare = #"Share \r";
NSURL *url = newsC.URL;
NSArray *objectsToShare = #[textToShare, url];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
//this array should add the activities that I don’t want
NSArray *excludeActivities = #[ UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo
];
activityVC.excludedActivityTypes = excludeActivities;
[self presentViewController:activityVC animated:YES completion:nil];
Thanks in advance
You need to use below method:
-(void)instaPostImage
{
NSURL *myURL = [NSURL URLWithString:#"Your image url"];
NSData * imageData = [[NSData alloc] initWithContentsOfURL:myURL];
UIImage *imgShare = [[UIImage alloc] initWithData:imageData];
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not
{
UIImage *imageToUse = imgShare;
NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:#"Image.png"];
NSData *imageData=UIImagePNGRepresentation(imageToUse);
[imageData writeToFile:saveImagePath atomically:YES];
NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
self.documentController=[[UIDocumentInteractionController alloc]init];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];
self.documentController.delegate = self;
self.documentController.annotation = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:#"Testing"], #"InstagramCaption", nil];
self.documentController.UTI = #"com.instagram.exclusivegram";
UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
[self.documentController presentOpenInMenuFromRect:CGRectMake(1, 1, 1, 1) inView:vc.view animated:YES];
}
else {
DisplayAlertWithTitle(#"Instagram not found", #"")
}
}
Try This
- (IBAction)shareBtnClicked {
NSString *textToShare = #"Share \r";
NSURL *url = newsC.URL;
NSData * imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *imgShare = [[UIImage alloc] initWithData:imageData];
NSArray *objectsToShare = #[imgShare, textToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
//this array should add the activities that I don’t want
NSArray *excludeActivities = #[ UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo
];
activityVC.excludedActivityTypes = excludeActivities;
[self presentViewController:activityVC animated:YES completion:nil];
}

UIActivityViewController social sharing (Facebook) only shows first Object in NSArray

I am trying to share content via Facebook throug the UIActivityViewController and it is working but not showing the correct data.
I am using the following code:
-(void)shareContent{
Item *nItem = [_items getItem:_currentPage];
NSString * urlString = #"http://www.foo.com";
NSString *url1=nItem.img;
NSMutableString *url2 = [[NSMutableString alloc]init];
[url2 appendString:url1];
[url2 appendString:800];
NSURL *url = [NSURL URLWithString:url2];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
NSArray * shareItems = #[urlString, img];
UIActivityViewController * avc = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];
[self presentViewController:avc animated:YES completion:nil];
}
But is only showing the urlString message, not the image.
Hope you can help with this.
UPDATE - EDIT
Is working fine in Notes, it shows both of them so I think I should something different for Facebook.

UIActivityViewController won't post URL to Twitter and Facebook

I'm using the UIActivityViewController to share some text, a URL, and an image to email, Twitter, Facebook, etc.
// Define parameters
NSString *text = #"example text";
NSURL *url = [NSURL URLWithString:#"www.google.com"];
UIImage *image = self.myImage;
// Create and display activity view controller
NSArray *activityItems = #[text, url, image];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
The text and image appear in the email and get posted to Twitter/Facebook, but the URL doesn't show up anywhere. Any ideas what the problem could be?
NSURL *url = [NSURL URLWithString:#"www.google.com"];
Is not a valid URL. It does not have a scheme. Try:
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];

Share PDF file on iOS

I Would like to know if i could use UIActivityViewController so i can share a PDF file by email, or message, or other means possible.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:linkToProspect]];
This is my PDF data that i would like to share.
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:*NSARRAY* applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
And this is the way i would like to do it.
Is there such a way ? Or should i create something custom ?
// pathToPDF should be a full path to your PDF file
NSString *pathToPDF = [[NSBundle mainBundle] pathForResource:#"ReadMe" ofType:#"pdf"];
NSURL *urlToPDF = [NSURL fileURLWithPath:pathToPDF];
NSMutableArray *itemsToShare = [[NSMutableArray alloc] init];
[itemsToShare addObject:urlToPDF];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];

UIActivityViewController sharing image via email has no extension

The image attached is just "Attachment-1", no extension. How do I specify one ?
NSData *compressedImage = UIImageJPEGRepresentation(self.resultImage, 0.8 );
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[ #"Check this out!", compressedImage ] applicationActivities:nil];
[self.navigationController presentViewController:activityViewController animated:YES completion:nil];
According to this answer you should be able to use this workaround to specify a filename
NSData *compressedImage = UIImageJPEGRepresentation(self.resultImage, 0.8 );
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [docsPath stringByAppendingPathComponent:#"image.jpg"];
NSURL *imageUrl = [NSURL fileURLWithPath:imagePath];
[compressedImage writeToURL:imageUrl atomically:YES]; // save the file
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[ #"Check this out!", imageUrl ] applicationActivities:nil];
The obvious drawback of this approach is that you will have to save the image on disk.
Credit to timeuser's suggestion, converting UIImage to NSData.
NSData * data = UIImageJPEGRepresentation(original,1.0);
UIActivityViewController * activity =[[UIActivityViewController alloc] initWithActivityItems:#[#"Your Text", data] applicationActivities:nil];
[self presentViewController:activity animated:true completion:^{ }];

Resources