Share PDF file on iOS - 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];

Related

How to share Animated gif

This is my code for sharing gif but i'm not able to share animated gif, i,m getting only image.
NSData *animatedGif = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:fileName]];
NSArray *sharingItems = [NSArray arrayWithObjects: animatedGif, nil];
NSLog(#"%#", sharingItems);
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];

Share Audio file using UIActivityViewController

I am not able to see any item in UIActivityViewController. I want to share Audio file to Available Sharing options in iPhone device.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html
Review this doc, it will help you.
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *filePath = [docPath stringByAppendingPathComponent:#"sound.aac"];//Audio file
NSURL *fileUrl = [NSURL fileURLWithPath:filePath isDirectory:NO];
NSArray *activityItems = #[fileUrl];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
Above answer worked for me after little bet struggle.I think there is some issue to access file path .here is modified and tested code.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sampleFile" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSArray *activityItems = #[soundFileURL];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];

Twitter icon not showing on uiactivitycontroller in ios 7

i configured twitter properly in setting menu, my problem is only twitter string is showing on UIActivityViewController, can anyone help me how can i display twitter icon?
NSString *shareString = #"Welcome.";
UIImage *shareImage = [UIImage imageNamed:#"welcome.png"];
NSURL *shareUrl = [NSURL URLWithString:#"http://www.welcome.com"];
NSArray *activityItems = [NSArray arrayWithObjects:shareString, shareImage, shareUrl, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
activityViewController.excludedActivityTypes = #[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact];
[self presentViewController:activityViewController animated:YES completion:nil];

ios - UIActivityViewController: SMS option not showing

I am using UIActivityViewController to allow sharing by Mail, Facebook, Twitter and SMS. I have the following code:
-(void) aFunction
{
NSString *textToShare = appRecord.title;
UIImage *imageToShare = [UIImage imageNamed:#"test/png"];
NSURL *url = [NSURL URLWithString:#"http://www.stackoverflow.com"];
NSArray *activityItems = [[NSArray alloc] initWithObjects:textToShare, imageToShare,url,nil];
UIActivity *activity = [[UIActivity alloc] init];
NSArray *applicationActivities = [[NSArray alloc] initWithObjects:activity, nil];
UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:activityItems
applicationActivities:applicationActivities];
activityVC.excludedActivityTypes = #[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint ,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeMessage ];
[self presentViewController:activityVC animated:YES completion:nil];
[activityItems release];
[activity release];
[applicationActivities release];
[activity release];
}
The code produces the following:
http://s24.postimg.org/tas3zo9w5/Screen_Shot_2013_10_01_at_5_09_26_PM.png
The SMS option is missing. What I am doing wrong?
you are excluding UIActivityTypeMessage (excludedActivityTypes). This hides the Messages (SMS/iMessage) option

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