Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 4 - UIActivityViewController share to Outlook Subject and Title

I am using UIActivityViewController to share text to Outlook, everything is working fine except for two things, one I can't set a subject line and two I cant set a title of that I am sharing, my text is html table and it is currently shown the first part of it in my implementation (see screenshot) although I'd like to show a preview title.

Is there a way to set the title and a way to set the subject?

Here is my code:

let activityViewController = UIActivityViewController(activityItems : [htmlString], applicationActivities: nil)

activityViewController.popoverPresentationController?.barButtonItem = self.shareButton

self.present(activityViewController, animated: true, completion: nil)

I have tried to set the subject like so:

activityViewController.setValue("This is my subject", forKey: "subject")

But it didn't work

What am I doing wrong?

Here is the screenshot of the title I was talking about:

enter image description here

But it didn't work

like image 331
user979331 Avatar asked Mar 04 '23 01:03

user979331


1 Answers

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.

like image 158
denis_lor Avatar answered Mar 05 '23 16:03

denis_lor