Swift 4 - UIActivityViewController share to Outlook Subject and Title
Whatever view controller triggered the sharing should conform to UIActivityItemSource
protocol in order to setup: preview title, email subject and as well the content of the email.
You can try out your self this example which triggers sharing on a button tap:
class ViewController: UIViewController, UIActivityItemSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func sharePressed(_ sender: Any) {
let item = [self, "Preview Title"] as [Any]
let activityViewController = UIActivityViewController(activityItems: item, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
}
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return ""
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
return "<html><body><p style=\"background-color: red;\">Email body message with red background</p></body></html>"
}
func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {
return "Email Subject"
}
}
To have this expected output:
Some apps like Outlook and Gmail in different iOS Versions (iOS 11, iOS 12..) behave differently: some take the first line of the body and set it as the Subject. Just be sure to test it out for the iOS version you are targeting and the app that you want to target the sharing functionality to behave correctly. You could also set multiple itemForActivityType
to target logic for different apps when sharing.
How to set a mail Subject in UIActivityViewController?
It seems as though emreoktem's solution—sending setValue:forKey:
to the UIActivityViewController
—is undocumented.
On iOS 7 and later, you can implement the activityViewController:subjectForActivityType:
method in an object conforming to the UIActivityItemSource
protocol to do this in a way that is documented.
Add subject on email by share UIActivityViewController in Swift
Just set value of subject to your title like this:
activityViewController.setValue("This is my title", forKey: "Subject")
Related Topics
How to Dismiss a Modal Segue Then Perform Push Segue to New View Controller
How Has The Nsindexpath Initialization Changed in Swift3
Are Private Frameworks Supported on iOS
How to Make a Phonegap App for iOS Without Mac
How to Add a Lock Screen Widget (Requiring iOS 16) and Still Support iOS 15
Strange Custom Background Color on Uipickerview Swift
Case Insensitive Matching Search in String Array Swift 3
iOS Swift Streaming App Does Not Play Music in Background Mode
iOS Framework Does Not Work on Simulator
Swiftui: Navigate to Home Screen After Login Completed. Navigating Views by Button Click
Passing Arguments to @Selector Method
Uibarbuttonitem Changing Title Not Working
Resize All Subview in Scrollviewdidzoom
Creating a First Launch Viewcontroller
CSS Flexible Box Layout on Ipad
How to Keep Animated Gifs Animated While Scrolling on iOS Devices