Sharesheet doesn't attach file in mail app (swift) - 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)

Related

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)

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)

UIActivityViewController share a video on WhatsApp

I have look around a lot but didn't find a solution to my problem.
I am displaying videos in a tableviewcell like this
if let videoUrlString = post?.videoUrl, let videoUrl = URL(string: videoUrlString) {
self.volumenView.isHidden = false
player = AVPlayer(url: videoUrl)
playerLayer = AVPlayerLayer(player: player)
playerLayer?.frame = postImageView.frame
playerLayer?.frame.size.width = UIScreen.main.bounds.width
playerLayer?.frame.size.height = UIScreen.main.bounds.width / post!.ratio!
self.contentView.layer.addSublayer(playerLayer!)
self.volumenView.layer.zPosition = 1
player?.play()
player?.isMuted = isMuted
NotificationCenter.default.addObserver(self, selector:#selector(self.playerDidFinishPlaying(note:)),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem)
}
this works perfectly fine.
Now I want the user to be able to share the video on WhatsApp.
I ve made it work text post and image post which work fine but wasn't able to make it run with videos.
this is how I currently send the notification
let videoURL = NSURL(fileURLWithPath: (post?.videoUrl)!)
let data:[String: NSURL] = ["video": videoURL]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "shareCalled"), object: nil, userInfo: data)
this works so far that the chat partner receives a link to where the video is stored in firebase and by clicking that link he can see the video.
This works but its not elegant, I would like to send the video as one and not as a link where the user has to suspiciously click on.
Any idea?
Edit: code where I share the post (notice: text and image work fine, video just sends the url but not a video as one
#objc func shareCalled(_ notification: NSNotification) {
if let data = notification.userInfo?["image"] as? UIImage {
let activityVC = UIActivityViewController(activityItems: [data], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.view
activityVC.excludedActivityTypes = [UIActivityType.airDrop, UIActivityType.addToReadingList,UIActivityType.openInIBooks, UIActivityType.openInIBooks]
self.present(activityVC, animated: true, completion: nil)
}
if let data = notification.userInfo?["caption"] as? String {
let activityVC = UIActivityViewController(activityItems: [data], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.view
activityVC.excludedActivityTypes = [UIActivityType.airDrop, UIActivityType.addToReadingList,UIActivityType.openInIBooks, UIActivityType.openInIBooks]
self.present(activityVC, animated: true, completion: nil)
}
if let data = notification.userInfo?["video"] as? URL {
let activityVC = UIActivityViewController(activityItems: [data], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.view
activityVC.excludedActivityTypes = [UIActivityType.airDrop, UIActivityType.addToReadingList,UIActivityType.openInIBooks, UIActivityType.openInIBooks]
self.present(activityVC, animated: true, completion: nil)
}
}

Unable to share PDF in Social media

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)

Share image and text and url in whatsapp

I have tried the following code but only text and url is shared. I have used UIActivityItemSource also but this uses either text or image. Is there any work around to achieve these?
var objectsToShare = [AnyObject]()
objectsToShare.append(self.postTitle!)
objectsToShare.append(self.postUrl!)
objectsToShare.append(self.postImage!)
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]
presentationContext.presentViewController(activityVC, animated: true, completion: nil)