Unable to share PDF in Social media - swift

When I tried sharing a text, I got all the apps that are available in my iPhone but when I try to share PDF, the only options that are visible are Mail and Whatsapp. Sharing on FB option was not there. I am unable to share the PDF in Whatsapp also even though the size is 86KB. I got the following error
"This item can't be shared. Try sharing some other item"
In the following link, it is possible to share on FB it seems.
How to Share PDF file in all eligable and appear iOS app?.
Can anyone give me some idea?. I tried the following code
func actionMenuViewControllerShareDocument(_ actionMenuViewController: ActionMenuViewController) {
self.dismiss(animated: true, completion: nil)
if let lastPathComponent = pdfDocument?.documentURL?.lastPathComponent,
let documentAttributes = pdfDocument?.documentAttributes,
let attachmentData = pdfDocument?.dataRepresentation() {
let shareText = attachmentData
let activityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
}
}

I used UIDocumentInteractionController instead of UIActivityViewController.The following code solved my issue in sending PDF to Whatsapp, Add to Notes, Mail,etc..
let url = Bundle.main.url(forResource: "Sample", withExtension: "pdf")!
docController = UIDocumentInteractionController(url: url)
docController?.delegate = self
docController?.presentOptionsMenu(from: self.view.frame, in:self.view, animated:true)

Related

Sharesheet doesn't attach file in mail app (swift)

I need to share a file (it can be pdf, xlsx or an image) using a Sharesheet. It works on whatsapp or other apps but when I select "mail" only opens mail app but without any attachment. Here is my code:
let myURL = URL(fileURLWithPath: fileURLString)
let activityItems: [AnyObject] = [myURL as AnyObject]
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView=self.view
self.present(activityViewController, animated: true, completion: nil)
(I am running it in the main thread from an asynchronous task)

Unable to get share menu to work with PDF data using Mac Catalyst

I am attempting to use a UIActivityViewController to create a share menu using PDF data using the code below. (The type is 'Data', I also attempted NSData to no avail):
let activityViewController = UIActivityViewController(
activityItems: [documentData],
applicationActivities: nil)
It works fine on iPhone and iPad but when running on macOS I only get "More..."
By comparison here's how the exact same file behaves on iOS:
You need to save the data to a file and share the URL of that file.
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = documentsURL.appendingPathComponent("file.pdf")
try? pdfData.write(to: fileURL)
let activityController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
present(activityController, animated: true, completion: nil)

UIImageView share with UIActivityViewController

I have this code for get my image
imgurl.image = UIImage.init(ciImage: transformedImage)
the image great show in device now I want to share this image with UIActivityViewController
here is my code
if (self.imgurl.image == nil) {
NSLog("image not available.")
return
}
else
{
let activityItem: [AnyObject] = [self.imgurl.image as AnyObject]
let avc = UIActivityViewController(activityItems: activityItem as [AnyObject], applicationActivities: nil)
self.present(avc, animated: true, completion: nil)
}
but after try to share I received this error for Email :
[MFMailComposeInternalViewController addAttachmentData:mimeType:fileName:] attachment must not be nil.'
and for Whats app I received this item cannot be shared.Please select a different item
is it need to save the image in document directory and need to get from document directory if yes I can't write a code too for saving.

Swift 4 UIActivityViewController send an Image with AirDrop

I have a problem, I would like to sent an Image with E-Mail, Message and also AirDrop. Currently only E-Mail and Message works, when I try to sent it per AirDrop, the Device get an .data File, which I can't open. How I can fix this problem ?
#IBAction func sendItButton(_ sender: UIBarButtonItem) {
let imageShare = [ image! ]
let activityViewController = UIActivityViewController(activityItems: imageShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.barButtonItem = sender
self.present(activityViewController, animated: true, completion: nil)
}
I have faced similar issue today, seems like there's a bug on UIActivityViewController if the UIImage is originated from a .jpeg image from bundle. I used the file URL of the image (instead of UIImage) as the activityItems and it worked fine.
private func urlForBundleFile(filename: String) -> URL? {
let components = filename.components(separatedBy: ".")
return Bundle.main.url(forResource: components[0], withExtension: components[1])
}
let imageURL = urlForBundleFile(filename: "someImage.jpeg")
let activityViewController = UIActivityViewController(activityItems: [imageURL] , applicationActivities: nil)

How to share a video from UIActivityViewController to Vimeo or Facebook using Swift

I'm trying to share a video to Vimeo and or Facebook. I'm able to do it from the Library, but when I try to create it in code, it fails this far I have tried this
func showSheet() {
let bundle = NSBundle.mainBundle()
let videoURL:NSURL = NSURL.fileURLWithPath(self.outputUrl!)!
//let asset:AVAsset = AVAsset.assetWithURL(videoURL) as AVAsset
let asset:ALAsset = ALAsset.setVideoAtPath(videoURL)
//NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/image.mov"];
//NSURL *videoURL = [NSURL fileURLWithPath:myFilePath];
let objectsToShare = [textToShare, asset]
//let objectsToShare = [textToShare, videoURL]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityTypeAddToReadingList]
self.presentViewController(activityVC, animated: true, completion: nil)
}
As you can see from the commented lines, I have tried passing an NSURL, An AVAsset and, and ALAsset, the only one that works is the NSURL. I am able to send it via iMessage, but it does not work for Facebook. Furthermore, Vimeo and YouTube never show up in the list, even though they show up when I trigger the sheet from the photo Library. What am I doing wrong? Following the data grid at the bottom of this page I should be able to pass an asset.