UIImageView share with UIActivityViewController - swift

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.

Related

Sharing multiple images AND text using activityController is not working

I am making a sales app to email charts as images with text for my company, but I am having a problem with my app.
For some reason I can't send multiple images and text together, when the code below is activated, only the text is sent.
I triple checked and the images are NOT nil and I can share text, multiple images alone or text and a single image just fine.
Furthermore I am only sending two images and this is just for the phone version alone.
func EmailMultipleImages(imageArray: [UIImage], emailSubject: String, emailBodyText : String) {
print("Image array \(imageArray.count)")
do {
let shareContent: [Any] = [imageArray, emailBodyText]
//Multiple images alone
//let shareContent: [Any] = [imageArray]
//Text alone
//let shareContent: [Any] = [emailBodyText]
//One image and text
//let shareContent: [Any] = [imageArray[0], emailBodyText]
let activityController = UIActivityViewController(activityItems: shareContent, applicationActivities: nil)
activityController.setValue(emailSubject, forKey: "Subject")
viewController!.present(activityController, animated: true, completion: nil)
}
catch {
print("Error print mulitple images \(error)")
}
}
Make that array a flat object and it should work.
example:
func share() {
let activityItems = [
"Title",
"Body",
UIImage(systemName: "keyboard")!,
UIImage(systemName: "square.and.arrow.up")!
] as [Any]
let vc = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
vc.setValue("Testing", forKey: "Subject")
self.present(vc, animated: true, completion: nil)
}
Output:

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)

UIActivityViewController send UIImage

I have one image generate it in App
the image displayed in uiimageview
here is my image
var img:UIImage!
img = UIImage(ciImage: transformedImage)
here is the value before share it in breakpoint
print(img)
some(UIImage: 12345 , {200, 200})
let myShare = "My beautiful photo! <3 <3"
let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [(img), myShare], applicationActivities: nil)
self.present(shareVC, animated: true, completion: nil)
but the image can not be show but the text it's show in iMessage or email and other apps ... How can I share the image ? in iMessage emails and etc...
Use this method:
func shareImageBtn(img : UIImage, txt : String) {
// if an image and a txt aren't nil
let resourses = [ image!, txt!]
let activityViewController = UIActivityViewController(activityItems: resources, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // iPads won't crash
// (optional)
activityViewController.excludedActivityTypes = [ .airDrop, .postToFacebook ]
// present the VC
self.present(activityViewController, 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)

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)