UIActivityViewController not showing all options - swift

In a tableview, I am using a context menu to share a cell's image (via snapshot()) and a small string. However, when I use the UIActivityViewController, not all of the options like Mail and Messages are showing.
What am I doing wrong here? How can I show all of the share options?
if let cell = tableView.cellForRow(at: indexPath) as? ResultsTableViewCell,
let cellSnapshot = cell.snapshot() {
let items: [Any] = ["Check out this speed test.", cellSnapshot]
let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
self.present(activityViewController, animated: true)
}

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:

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)
}

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)

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)