How to share multiple file via UIDocumentInteractionController? - ios

I am trying to implement share multiple file to Facebook , mail ,google driver and whatsapp.
I can share one file via UIDocumentInteractionController like the following code:
NSString *filePath = [NSString stringWithFormat:#"%#/%#", directory, [fileList objectAtIndex:selectedIndexPath.row]] ;
url = [NSURL fileURLWithPath: filePath] ;
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
[documentInteractionController setDelegate:self];
documentInteractionController.UTI = #"net.whatsapp.image";
[documentInteractionController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES];
How to share multiple file via UIDocumentInteractionController without using UIActivityViewController ?
If use NSMutableArray , and add multiple object of url. How to set NSMutableArray to UIDocumentInteractionController ?
Thanks in advance.

To share multiple files you can use UIActivityViewController.
UIImage *image = [UIImage imageWithCGImage:self.imgView.image.CGImage];
NSArray* dataToShare = #[image, image2, image3]; // Any data you want to share.
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
This might help you.

Related

iOS - export multiple pdf

In my app, I want to implement the ability to export the multiple pdf files.
For now, I can export an only pdf file using the code below:
// get local path url
NSURL *url = [self getFileURLWithIndexPath:indexPath];
if(url) {
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[url path]];
if(!fileExists) {
NSLog(#"%#", [url path]);
}
self.docController = [UIDocumentInteractionController interactionControllerWithURL:url];
[self.docController setDelegate:self];
BOOL canOpenFile = [self.docController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
if(!canOpenFile) {
// No reader PDF
}
}
The url is the local URL path.
This method will popup where I can choose iBooks to export.
But I have no idea how to export multiple files, can anyone help me...?
Thank!
You can use UIActivityViewController to export multiple files and can open in UIDocumentationInteractionController, it provides in-app support for managing user interactions with files in the local system.
NSArray *dataItems = #[pdf1, pdf2, pdf3];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataItems
applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:^{
}];

How to share text and image(both) on whatsapp using UIActivityController : Objective C?

UIImage * imageShare = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:trackImagePath]]];
NSURL *urlLink = [NSURL URLWithString:linkstr];
SharingActivityProvider * share = [[SharingActivityProvider alloc]initWithPlaceholderItem:#"Ghaneely Share!"];
share.sharelink = urlLink;
share.sharelinkText = shareString;
NSArray *objectsToShare = #[imageShare,share];
activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
NSArray *excludeActivities = #[UIActivityTypePostToWeibo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypeAssignToContact,UIActivityTypeAirDrop];
activityVC.excludedActivityTypes = excludeActivities;
[self presentViewController:activityVC animated:TRUE completion:nil];
I want to share text and image (both) on WhatsApp just like Android by using UIActivityController. It used to work before when I used share without SharingActivityProvider. But I need to post different text on facebook, twitter,etc ,hence I am using ActivityProvider(which I think should not be the problem). Now with the code above it is sharing only image.
What could be the issue?
From this way you can share text and video to whatsapp :-
NSString *URLString=#"http://video.app.com/video.php?";
NSURL *VideoURL=[NSURL URLWithString:#“URLString ”];
NSMutableArray *activityItems= [NSMutableArray arrayWithObjects:VideoURL,#"Hey, check out this video I've shared with you, it's awesome!", result1, nil];
self.activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
self.activityViewController.excludedActivityTypes = #[UIActivityTypePostToWeibo,UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,UIActivityTypeAirDrop];
[self presentViewController:self.activityViewController animated:YES completion:nil];
try this may be it will help you.

Whatsapp not picking the video from the assetURL in UIActivityViewController

I am trying to upload a video to Whatsapp with an ALAsset URL, but it is loading the video when I share it using UIActivityViewController
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:self.videoURL resultBlock:^(ALAsset *asset) {
NSArray *objectsToShare = #[asset.defaultRepresentation.url];
FrodoInstagramActivity *instagramActivity = [[FrodoInstagramActivity alloc] init];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:objectsToShare applicationActivities:#[instagramActivity]];
[self presentViewController:activityVC animated:YES completion:nil];
} failureBlock:nil];
Try this noh, "Movie" Your video which saved to local directory from video asset.
NSString * savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wam"];
savePath = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = #"net.whatsapp.movie";
_documentInteractionController.delegate = (id)self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
Save your asset locally, and get the local URL. Pass that URL path on the above code. Let's see.
Dont create each file locally for each video, just use the same file name for all videos and replace videos. And delete the video from local, after successfully uploaded. This might help.
I have also face this problem with sharing the link on whatsapp.
In whatsapp sharing was restrit to number of characters.
Please share url and text with maximum 150 characters.
then say what happen.
happy coding :)

UIActivityViewController or UIDocumentInteractionController with WhatsApp and FB

I need to have Facebook and WhatsApp as sharing options for my image. I've already implemented UIActivityViewController, where i can share via Facebook and UIDocumentInteractionController where i can share via WhatsApp. I don't know how to merge these things.
UIActivityViewController:
UIActivityViewController *activityViewContoller = [[UIActivityViewController alloc]
initWithActivityItems:#[#"Test", image] applicationActivities:nil];
[self presentViewController:activityViewContoller animated:YES completion:nil];
UIDocumentInteractionController:
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wai"];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:savePath atomically:YES];
_documentInteractionController = [UIDocumentInteractionController
interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = #"net.whatsapp.image";
_documentInteractionController.delegate = self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectZero
inView:self.view animated:YES];
I want to have both of them in one popover, however I have no idea how to achieve it. Any tip please?
I've checked out StackOverFlow question 1, but it doesn't help me at all. My file is .wai (for WhatsApp) so when i try to send it via FB file is unable to open. Also it shows all options, while i want only 2(FB+WhatsApp) to be visible. Following the StackOverFlow question 2 I can show only FB (working one, because i set normal image) but can't add WhatsApp (no .wai file, i don't know what to do with UTI). Is there any way to solve this issue?
To change type of file:
- (void)share {
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/tmptmpimg.jpg"];
[UIImageJPEGRepresentation(_img, 1.0) writeToFile:path atomically:YES];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];
_documentInteractionController.delegate = self;
[_documentInteractionController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES];
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
if ([self isWhatsApplication:application]) {
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/tmptmpimg.wai"];
[UIImageJPEGRepresentation(_img, 1.0) writeToFile:savePath atomically:YES];
controller.URL = [NSURL fileURLWithPath:savePath];
controller.UTI = #"net.whatsapp.image";
}
}
- (BOOL)isWhatsApplication:(NSString *)application {
if ([application rangeOfString:#"whats"].location == NSNotFound) { // unfortunately, no other way...
return NO;
} else {
return YES;
}
}
This way we can use all options- Facebook, Twitter and custom WhatsApp.
The problem with showing only selected options is still not solved, but it's the minor one.
To exclude non-desired sharing options (the second part of your question), assuming your UIActivityViewController object is called activityController, set the excludedActivityTypes property, like so:
activityController.excludedActivityTypes = #[UIActivityTypeAssignToContact,
UIActivityTypePrint,
UIActivityTypeAddToReadingList,
UIActivityTypeAirDrop];

UIActivityViewController Automatically 'Clicking' Link iOS 7

I have a basic UIActivityViewController sharing some text and a URL. The sharing options are Email, SMS, Twitter, and Facebook. Everything works fine in iOS 6 but in iOS 7 when I choose Twitter or Facebook, without touching anything my device automatically closes my app, opens Safari and goes to the URL I am trying to share. What could be causing this?
Code (CustomActivityItemProvider is a subclass to customize text based on sharer):
if ([UIActivityViewController class]) { // if class exists = ios6+
// Create array for sharing
CustomActivityItemProvider *textToShare = [[CustomActivityItemProvider alloc]
initWithStandardText:#"Text to share.\n"];
NSURL *urlToShare = iTunesShort ? iTunesShort : [NSURL URLWithString:iTunesLink];
NSArray *activityItems = #[textToShare, urlToShare];
// Show sharing view
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:TRUE completion:nil];
}
So I still don't know why only shortened URLs were causing this problem, but a solution that took care of it was to include the urlToShare in the textToShare.
// Create array for sharing
NSString *urlToShare = iTunesShort ? iTunesShort : iTunesLink;
NSString *textWithUrl = [NSString stringWithFormat:#"Text to share.\n%#", urlToShare];
CustomActivityItemProvider *textToShare = [[CustomActivityItemProvider alloc]
initWithStandardText:textWithUrl];
NSArray *activityItems = #[textToShare];

Resources