How to set a mail Subject in UIActivityViewController?

IosObjective CEmailUiactivityviewcontrollerSubject

Ios Problem Overview


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"?

Ios Solutions


Solution 1 - Ios

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...

Solution 2 - Ios

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.

Solution 3 - Ios

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)

Solution 4 - Ios

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFeminaView Question on Stackoverflow
Solution 1 - IosemreoktemView Answer on Stackoverflow
Solution 2 - IosTim ArnoldView Answer on Stackoverflow
Solution 3 - IosbiomikerView Answer on Stackoverflow
Solution 4 - IosPACView Answer on Stackoverflow