Uiactivityviewcontroller Subject Not Working for Outlook

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



Leave a reply



Submit