Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rajatmohanty/86aae6c33e3233ddf223d8654fd2f3fa to your computer and use it in GitHub Desktop.
Save rajatmohanty/86aae6c33e3233ddf223d8654fd2f3fa to your computer and use it in GitHub Desktop.
import UIKit
class RootViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let text = "Cute cat"
let image = UIImage(named: "cat.jpg")!
// Note: you can't wrap the text and image into a single "TextImage" object and selectively return both or one as UIActivityItemSource doesn't like holding arrays
let activityViewController = UIActivityViewController(activityItems: [image, OptionalTextActivityItemSource(text: text)], applicationActivities: nil)
navigationController?.present(activityViewController, animated: true, completion: nil)
}
}
class OptionalTextActivityItemSource: NSObject, UIActivityItemSource {
let text: String
init(text: String) {
self.text = text
}
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return text
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
if activityType?.rawValue == "net.whatsapp.WhatsApp.ShareExtension" {
return nil
} else {
return text
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment