74

I want to set subject for email sharing in UIActivityViewController and also want to share in Twitter. I know in Twitter if we want to share — we need compress text to 140 chars. I checked many SO solutions, but nothing is working.

Is this issue fixed in latest iOS releases? Any other "working solutions"?

1
  • No it dint. Just check my comment below and let me know if i miss the right way. thank you
    – Femina
    Oct 30, 2013 at 10:28

4 Answers 4

97

Check below code for the email for setting up your email subject:

UIActivityViewController* avc = [[UIActivityViewController alloc] initWithActivityItems:@[@"Your String to share"]
                                  applicationActivities:nil];
[avc setValue:@"Your email Subject" forKey:@"subject"];

avc.completionHandler = ^(NSString *activityType, BOOL completed) {
    // ...
};

Here the line

[avc setValue:@"Your email Subject" forKey:@"subject"];

Makes the subject as "Your email Subject" if user picks email option in the UIActivityViewController.

I hope it helps...

7
  • 1
    [activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) { if([activityType isEqualToString: UIActivityTypeMail]){ NSLog(@"Mail type selected"); [activityVC setValue:@"Share!" forKey:@"subject"]; } }]; Its not working emreoktem!
    – Femina
    Oct 30, 2013 at 10:12
  • @Meenu You should set the subject before the completion handler.
    – colinta
    Apr 18, 2014 at 17:02
  • 3
    This doesn't seem to be working in iOS8, is there another way of doing this?
    – svarrall
    Nov 6, 2014 at 15:07
  • 1
    This and the activityViewController:subjectForActivityType: are annoyingly setting email subject and navigation bar title to the same string. It would be nice to have a official and documented way to do that.
    – Claus
    May 20, 2015 at 13:49
  • 2
    but email subject is not showing when I am opening gmail application.I am getting Body content in Gmail Subject section Oct 25, 2016 at 14:31
95

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.

5
  • 8
    Yes, activityViewController:subjectForActivityType: is the correct solution. Strangely the iOS Mail app uses the subject correctly but Google Inbox uses the message for the subject as well (not the subject supplied via subjectForActivityType) Jul 22, 2015 at 5:46
  • 3
    @LeslieGodwin Did you found any solution for sharing via gmail? Apr 18, 2016 at 5:30
  • @TimCamber Though I have tried above way it is still showing problem via sharing in gmail. Please help to resolve Apr 18, 2016 at 5:31
  • @jalakpatel I'm not sure what you are asking. Are you sharing something to the Google Gmail app? In that case, it may not be UIActivityViewController's fault that the Gmail app is not picking up activityViewController:subjectForActivityType:.
    – Tim Arnold
    Apr 18, 2016 at 18:54
  • Thank you, this is the correct answer. I've added an answer with a concrete class for Swift 3+ that I found very useful.
    – biomiker
    Mar 21, 2018 at 18:20
32

Here's a concrete solution for Swift 3.0+ based on the accepted answer. Note that, like the accepted answer, this is known to work only on the iOS Mail app and not necessarily other apps.

Implementation:

class MessageWithSubject: NSObject, UIActivityItemSource {

    let subject:String
    let message:String

    init(subject: String, message: String) {
        self.subject = subject
        self.message = message

        super.init()
    }

    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return message
    }

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
        return message
    }

    func activityViewController(_ activityViewController: UIActivityViewController,
                                subjectForActivityType activityType: UIActivityType?) -> String {
        return subject
    }
}

Usage:

Here's an example of usage. Note that it works well to use this as the first item in the activityItems array, and include any additional items to follow:

let message = MessageWithSubject(subject: "Here is the subject", message: "An introductory message")
let itemsToShare:[Any] = [ message, image, url, etc ]

let controller = UIActivityViewController(activityItems: itemsToShare, applicationActivities: nil)
3
  • 1
    The following solution doesn't work for Gmail app but only for ios Mail app. On Gmail the body is being shown the same as a message subject
    – Gal Bracha
    Aug 30, 2018 at 9:40
  • That's true Gal, just like the accepted answer on which it is based. I added a comment to make that more explicit.
    – biomiker
    Sep 1, 2018 at 6:01
  • 1
    Class name should start with capital letter. Sep 9, 2018 at 19:33
19

For Swift 2.0+ & ios 8.0+

let title = "Title of the post"
let content = "Content of the post"
let objectsToShare = [title, content]

let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)      
activityVC.setValue(title, forKey: "Subject")
self.presentViewController(activityVC, animated: true, completion: nil)
3
  • does this work for sharing a link to a website? like [title, content, link] or does the URL has to be part of the content itself and let the email client parse it and make it clickable?
    – gadget00
    May 16, 2016 at 3:52
  • Per the accepted answer, this is undocumented and unreliable.
    – biomiker
    Mar 21, 2018 at 18:21
  • One suggestion is to leave the 'title' off the objectsToShare, since adding the title in the setValue sets the Subject in the Email already. Works for me.
    – AndiAna
    Feb 20, 2021 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.