Share Audio file using UIActivityViewController - ios

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];

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 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"];

UIActivityViewController and ALAsset

I have a task to share video from Assets Library to Vimeo. I'm implementing activity controller this way:
NSArray *activityItems = #[_videoAsset.defaultRepresentation.url];
UIActivityViewController *activity = [[UIActivityViewController alloc]
initWithActivityItems:activityItems
applicationActivities:nil];
activity.excludedActivityTypes = #[
UIActivityTypeAirDrop,
UIActivityTypeMessage,
UIActivityTypePostToFacebook,
];
[self presentViewController:activity animated:YES completion:nil];
Apple documentation says that I can share my ALAsset or URL as activity item, but I have only "Save Video" option in controller. So what am I doing wrong?
Had this problem too, eventually managed to share after saving ALAsset's data as a file and sharing it.
The code looks like this:
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary assetForURL:videoUrl resultBlock: ^(ALAsset *asset){
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* videoPath = [documentsDirectory stringByAppendingPathComponent:#"VIDEO.MOV"];
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:videoPath error:&error];
BOOL success = [data writeToFile:videoPath atomically:YES];
if (success) {
NSArray *activityItems = [NSArray arrayWithObjects:[NSURL fileURLWithPath:videoPath], nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
}
} failureBlock:nil];

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